Mutating cache crashing immediately

I am trying to mutate my cache, but it is crashing immediately when using ApolloStore.withinReadWriteTransaction(_:callbackQueue:completion:) and need help troubleshooting as I don’t know what is causing this issue. I referenced the Caching section of the documentation.

I’ve tried all sorts of fixes, but it always crashes on the same line of code. Please help!

Apollo 1.17.0
XCode 16.2

Below are some implementation details:

Client

    private(set) lazy var apollo: ApolloClient = {
        let client = URLSessionClient()
        let cache = InMemoryNormalizedCache()
        let store = ApolloStore(cache: cache)
        let provider = NetworkInterceptorProvider(client: client, store: store)
        let url = URL(string: AppInfo.defaultServer)!
        let transport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: url)

        return ApolloClient(networkTransport: transport, store: store)
    }()

Apollo Interceptor

class apolloInterceptorClassName: ApolloInterceptor {
    public var id: String = UUID().uuidString
    
    func interceptAsync<Operation>(
        chain: RequestChain,
        request: HTTPRequest<Operation>,
        response: HTTPResponse<Operation>?,
        completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void
    ) where Operation : GraphQLOperation {
        if let token = Keychain.shared.keychain.get(AppInfo.userTokenKey) {
            request.addHeader(name: "Authorization", value: token)
        }
        print(request.additionalHeaders)
        chain.proceedAsync(
            request: request,
            response: response,
            interceptor: self,
            completion: completion)
    } 
}
class interceptorProviderClassName: DefaultInterceptorProvider {    
    override func interceptors<Operation>(for operation: Operation) -> [ApolloInterceptor] where Operation : GraphQLOperation {
        var interceptors = super.interceptors(for: operation)
        interceptors.insert(AuthorizationInterceptor(), at: 0)
        return interceptors
    }
}

Schema Configuration

public enum SchemaConfiguration: ApolloAPI.SchemaConfiguration {
    public static func cacheKeyInfo(for type: ApolloAPI.Object, object: ApolloAPI.ObjectData) -> CacheKeyInfo? {
        switch type {
        default:
            guard let id = object["id"] as? Int else {
                return nil
            }
            return CacheKeyInfo(id: String(id), uniqueKeyGroup: type.typename)
//            return try? CacheKeyInfo(jsonValue: object["id"])
        }
    }
}

Mutable Fragment Definition

fragment MutableMediaDetails on Media
@apollo_client_ios_localCacheMutation {
  id
  isFavourite
}

I am able to fetch the data fine. When I perform the mutation, the data is updating on the server correctly, but after completing the mutation and I try to update the cache, the app crashes immediately.

ApolloStore.swift line 153 crash

  public func withinReadWriteTransaction<T>(
    _ body: @escaping (ReadWriteTransaction) throws -> T,
    callbackQueue: DispatchQueue? = nil,
    completion: ((Result<T, any Swift.Error>) -> Void)? = nil
  ) {
    self.queue.async(flags: .barrier) {
      do {
        let returnValue = try body(ReadWriteTransaction(store: self)) <-- CRASHES HERE!!
        
        DispatchQueue.returnResultAsyncIfNeeded(
          on: callbackQueue,
          action: completion,
          result: .success(returnValue)
        )
      } catch {
        DispatchQueue.returnResultAsyncIfNeeded(
          on: callbackQueue,
          action: completion,
          result: .failure(error)
        )
      }
    }
  }

Crash Report

Incident Identifier: 36AB5575-0FA0-47F8-BFFC-178E9BFBDCD5
CrashReporter Key:   862f3046fe8fa87903e12b90f3f24ffe48eac51f
Hardware Model:      iPhone16,1
Process:             [APP_NAME] [903]
Path:                /private/var/containers/Bundle/Application/2BC5407E-130B-4C6E-8F82-843226971FA1/[APP_NAME].app/[APP_NAME]
Identifier:          --BUNDLE-ID-PREFIX-.[APP_NAME]
Version:             1.0 (1)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           --BUNDLE-ID-PREFIX-.[APP_NAME] [1102]

Date/Time:           2025-01-13 07:18:18.5720 -0800
Launch Time:         2025-01-13 07:11:30.8456 -0800
OS Version:          iPhone OS 18.3 (22D5040d)
Release Type:        Beta
Baseband Version:    2.40.03
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001036f82a8
Termination Reason: SIGNAL 5 Trace/BPT trap: 5
Terminating Process: exc handler [903]

Triggered by Thread:  6

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0:
0   ???                                      0x1a6d51850 ???
1   Security                                 0x1a5ebd1cc der_decode_dictionary + 215
2   Security                                 0x1a5ebd7ac der_decode_plist + 1163
3   Security                                 0x1a5ebd1cc der_decode_dictionary + 215
4   Security                                 0x1a5ebd7ac der_decode_plist + 1163
5   Security                                 0x1a5ebd76c der_decode_plist + 1099
6   Security                                 0x1a5ebd1cc der_decode_dictionary + 215
7   Security                                 0x1a5ebd7ac der_decode_plist + 1163
8   Security                                 0x1a5ff3854 CFPropertyListCreateWithDERData + 95
9   Security                                 0x1a5fe8cbc SecTrustDeserialize + 55
10  WebKit                                   0x1b4108574 IPC::ArgumentCoder<WTF::RetainPtr<__SecTrust*>, void>::decode(IPC::Decoder&) + 123
11  WebKit                                   0x1b34b316c std::__1::optional<WTF::RetainPtr<__SecTrust*>> IPC::Decoder::decode<WTF::RetainPtr<__SecTrust*>>() + 27
12  WebKit                                   0x1b41111b0 std::__1::optional<std::__1::optional<WebKit::CoreIPCNSURLProtectionSpace>> IPC::ArgumentCoder<std::__1::optional<WebKit::CoreIPCNSURLProtectionSpace>, void>::decode<IPC::Decoder>(IPC::Decoder&) + 227
13  WebKit                                   0x1b40fe37c std::__1::optional<std::__1::optional<WebKit::CoreIPCNSURLProtectionSpace>> IPC::Decoder::decode<std::__1::optional<WebKit::CoreIPCNSURLProtectionSpace>>() + 27
14  WebKit                                   0x1b40fe2cc std::__1::optional<WTF::RetainPtr<objc_object*>> IPC::decodeObjectDirectlyRequiringAllowedClasses<NSURLProtectionSpace>(IPC::Decoder&) + 31
15  WebKit                                   0x1b34b5b4c IPC::ArgumentCoder<WebCore::AuthenticationChallenge, void>::decode(IPC::Decoder&) + 311
16  WebKit                                   0x1b343c8ac std::__1::optional<WebCore::AuthenticationChallenge> IPC::Decoder::decode<WebCore::AuthenticationChallenge>() + 27
17  WebKit                                   0x1b3624bc8 WebKit::NetworkProcessProxy::didReceiveNetworkProcessProxyMessage(IPC::Connection&, IPC::Decoder&) + 4595
18  WebKit                                   0x1b32c46f0 IPC::Connection::dispatchMessage(WTF::UniqueRef<IPC::Decoder>) + 251
19  WebKit                                   0x1b32c449c WTF::Detail::CallableWrapper<IPC::Connection::enqueueIncomingMessage(WTF::UniqueRef<IPC::Decoder>)::$_2, void>::call() + 123
20  JavaScriptCore                           0x1b4715158 WTF::RunLoop::performWork() + 203
21  JavaScriptCore                           0x1b471506c WTF::RunLoop::performWork(void*) + 35
22  CoreFoundation                           0x19d810f4c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 27
23  CoreFoundation                           0x19d810ee0 __CFRunLoopDoSource0 + 175
24  CoreFoundation                           0x19d813b40 __CFRunLoopDoSources0 + 243
25  CoreFoundation                           0x19d812d3c __CFRunLoopRun + 839
26  CoreFoundation                           0x19d865284 CFRunLoopRunSpecific + 587
27  GraphicsServices                         0x1eaab14c0 GSEventRunModal + 163
28  UIKitCore                                0x1a03b3350 -[UIApplication _run] + 815
29  UIKitCore                                0x19ffd9184 UIApplicationMain + 339
30  SwiftUI                                  0x1a2170eb0 closure #1 in KitRendererCommon(_:) + 167
31  SwiftUI                                  0x1a2170de0 runApp<A>(_:) + 99
32  SwiftUI                                  0x1a2170cc4 static App.main() + 179
33  [APP_NAME].debug.dylib                       0x105968070 static [APP_NAME]App.$main() + 40
34  [APP_NAME].debug.dylib                       0x105968e90 __debug_main_executable_dylib_entry_point + 12
35  dyld                                     0x1c3aa5de8 start + 2723

Thread 1 name:  com.apple.uikit.eventfetch-thread
Thread 1:
0   libsystem_kernel.dylib                   0x1eeeb4788 mach_msg2_trap + 8
1   libsystem_kernel.dylib                   0x1eeeb7e98 mach_msg2_internal + 79
2   libsystem_kernel.dylib                   0x1eeeb7db0 mach_msg_overwrite + 423
3   libsystem_kernel.dylib                   0x1eeeb7bfc mach_msg + 23
4   CoreFoundation                           0x19d813804 __CFRunLoopServiceMachPort + 159
5   CoreFoundation                           0x19d812eb0 __CFRunLoopRun + 1211
6   CoreFoundation                           0x19d865284 CFRunLoopRunSpecific + 587
7   Foundation                               0x19c3e6908 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 211
8   Foundation                               0x19c543310 -[NSRunLoop(NSRunLoop) runUntilDate:] + 63
9   UIKitCore                                0x1a0446978 -[UIEventFetcher threadMain] + 419
10  Foundation                               0x19c4d26a0 __NSThread__start__ + 723
11  libsystem_pthread.dylib                  0x2285057d0 _pthread_start + 135
12  libsystem_pthread.dylib                  0x228505480 thread_start + 7

Thread 2 name:  com.apple.NSURLConnectionLoader
Thread 2:
0   libsystem_kernel.dylib                   0x1eeeb44e4 kevent_id + 8
1   libdispatch.dylib                        0x10371a96c _dispatch_kq_poll + 228
2   libdispatch.dylib                        0x103719e78 _dispatch_event_loop_poke + 340
3   CFNetwork                                0x19ed85778 DiskCookieStorage::syncStorageWithCompletionLocked(unsigned char, void () block_pointer) + 4407
4   CFNetwork                                0x19ed84608 HTTPCookieStorage::syncStorageWithCompletion(unsigned char, void () block_pointer) + 183
5   CFNetwork                                0x19ee83598 HTTPCookieStorage::_syncTimerFired(__CFRunLoopTimer*, void*) + 147
6   CoreFoundation                           0x19d88d29c __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 31
7   CoreFoundation                           0x19d88cf40 __CFRunLoopDoTimer + 1011
8   CoreFoundation                           0x19d88ca94 __CFRunLoopDoTimers + 287
9   CoreFoundation                           0x19d813134 __CFRunLoopRun + 1855
10  CoreFoundation                           0x19d865284 CFRunLoopRunSpecific + 587
11  CFNetwork                                0x19ed8adfc +[__CFN_CoreSchedulingSetRunnable _run:] + 415
12  Foundation                               0x19c4d26a0 __NSThread__start__ + 723
13  libsystem_pthread.dylib                  0x2285057d0 _pthread_start + 135
14  libsystem_pthread.dylib                  0x228505480 thread_start + 7

Thread 3:
0   libsystem_pthread.dylib                  0x22850546c start_wqthread + 0

Thread 4:
0   JavaScriptCore                           0x1b451bdc0 bmalloc_small_segregated_page_config_specialized_local_allocator_return_memory_to_page + 4452
1   JavaScriptCore                           0x1b4519b8c pas_local_allocator_stop + 591
2   JavaScriptCore                           0x1b5a842e0 pas_thread_local_cache_stop_local_allocators + 467
3   JavaScriptCore                           0x1b5a84064 destructor + 191
4   libsystem_pthread.dylib                  0x228508fc4 _pthread_tsd_cleanup + 619
5   libsystem_pthread.dylib                  0x228508d34 _pthread_exit + 83
6   libsystem_pthread.dylib                  0x228508ce0 _pthread_wqthread_exit + 55
7   libsystem_pthread.dylib                  0x228507708 _pthread_wqthread + 423
8   libsystem_pthread.dylib                  0x228505474 start_wqthread + 7

Thread 5 name:   Dispatch queue: com.apple.network.connections
Thread 5:
0   libsystem_kernel.dylib                   0x1eeeb44e4 kevent_id + 8
1   libdispatch.dylib                        0x10371a96c _dispatch_kq_poll + 228
2   libdispatch.dylib                        0x10371a47c _dispatch_kq_drain + 164
3   libdispatch.dylib                        0x10371a334 _dispatch_event_loop_drain + 344
4   libdispatch.dylib                        0x103700ca4 _dispatch_workloop_invoke + 2912
5   libdispatch.dylib                        0x10370c6fc _dispatch_root_queue_drain_deferred_wlh + 328
6   libdispatch.dylib                        0x10370bd0c _dispatch_workloop_worker_thread + 580
7   libsystem_pthread.dylib                  0x228507680 _pthread_wqthread + 287
8   libsystem_pthread.dylib                  0x228505474 start_wqthread + 7

Thread 6 name:   Dispatch queue: com.apollographql.ApolloStore
Thread 6 Crashed:
0   libdispatch.dylib                        0x1036f82a8 _dispatch_assert_queue_fail + 120
1   libdispatch.dylib                        0x1036f8230 dispatch_assert_queue + 196
2   libswift_Concurrency.dylib               0x1a90f3b78 swift_task_isCurrentExecutorImpl(swift::SerialExecutorRef) + 283
3   [APP_NAME].debug.dylib                       0x105e96d38 closure #1 in closure #1 in DataService.updateCache(id:) + 208
4   [APP_NAME].debug.dylib                       0x105e97e88 partial apply for closure #1 in closure #1 in DataService.updateCache(id:) + 96
5   [APP_NAME].debug.dylib                       0x105eb3350 closure #1 in ApolloStore.withinReadWriteTransaction<A>(_:callbackQueue:completion:) + 324
6   [APP_NAME].debug.dylib                       0x105eaf264 thunk for @escaping @callee_guaranteed @Sendable () -> () + 48
7   libdispatch.dylib                        0x1036f488c _dispatch_call_block_and_release + 32
8   libdispatch.dylib                        0x1036f6578 _dispatch_client_callout + 20
9   libdispatch.dylib                        0x103709920 _dispatch_lane_concurrent_drain + 1144
10  libdispatch.dylib                        0x1036ff2cc _dispatch_lane_invoke + 520
11  libdispatch.dylib                        0x1036f8ed0 _dispatch_queue_override_invoke + 564
12  libdispatch.dylib                        0x10370ad34 _dispatch_root_queue_drain + 404
13  libdispatch.dylib                        0x10370b724 _dispatch_worker_thread2 + 188
14  libsystem_pthread.dylib                  0x228507644 _pthread_wqthread + 227
15  libsystem_pthread.dylib                  0x228505474 start_wqthread + 7

Thread 7:
0   libsystem_pthread.dylib                  0x228505478 thread_start + 0

Thread 8:
0   libsystem_pthread.dylib                  0x22850546c start_wqthread + 0

Thread 9:
0   libsystem_pthread.dylib                  0x22850546c start_wqthread + 0

Thread 10:
0   libsystem_pthread.dylib                  0x22850546c start_wqthread + 0


Thread 6 crashed with ARM Thread State (64-bit):
    x0: 0x00000003013c1e90   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x00000003013c1ec0
    x4: 0x00000003013c1f00   x5: 0x0000000000000000   x6: 0x0068637461707369   x7: 0x00000003013c1e80
    x8: 0x000000010375d000   x9: 0x000000009d827847  x10: 0x000000000000007f  x11: 0x0000000000001e80
   x12: 0x00000000000007fb  x13: 0x00000000000007fd  x14: 0x000000009da2803e  x15: 0x000000009d827847
   x16: 0x000000009da00000  x17: 0x000000000000003e  x18: 0x0000000000000000  x19: 0x00000003013c1a80
   x20: 0x0000000000000000  x21: 0x0000000000000000  x22: 0x0000000000030000  x23: 0x0000000000000000
   x24: 0x000000016d31f0e0  x25: 0x0000000302832bc0  x26: 0x0000000105eaf234  x27: 0x00000001036f486c
   x28: 0x0000000000000000   fp: 0x000000016d31e900   lr: 0x12710001036f82a0
    sp: 0x000000016d31e8c0   pc: 0x00000001036f82a8 cpsr: 0x60001000
   far: 0x0000000000000000  esr: 0xf2000001 (Breakpoint) brk 1

Binary Images:
       0x102b68000 -        0x102b73fff [APP_NAME] arm64  <a4b35b6baa1f3139bd9776c2b53ecb38> /private/var/containers/Bundle/Application/2BC5407E-130B-4C6E-8F82-843226971FA1/[APP_NAME].app/[APP_NAME]
       0x102b9c000 -        0x102b9ffff libLogRedirect.dylib arm64e  <f3df4371d9443962a606105bcd98e59c> /private/preboot/Cryptexes/OS/usr/lib/libLogRedirect.dylib
       0x102bc8000 -        0x102bcffff libBacktraceRecording.dylib arm64e  <9eebd0670d033b06b7352d123df1db47> /usr/lib/libBacktraceRecording.dylib
       0x102e40000 -        0x102e83fff libMainThreadChecker.dylib arm64e  <e5b5ad82f2d333eba9e5e77d9717c883> /usr/lib/libMainThreadChecker.dylib
       0x102f94000 -        0x10302ffff libRPAC.dylib arm64e  <eed646837af13125a1a480b92c861f8d> /usr/lib/libRPAC.dylib
       0x10364c000 -        0x103683fff libViewDebuggerSupport.dylib arm64e  <106f191cdd1f3916b3edf5e412ff5a7d> /usr/lib/libViewDebuggerSupport.dylib
       0x10595c000 -        0x10674ffff [APP_NAME].debug.dylib arm64  <96793e28476638ccaa6844cfe83b79a5> /private/var/containers/Bundle/Application/2BC5407E-130B-4C6E-8F82-843226971FA1/[APP_NAME].app/[APP_NAME].debug.dylib
       0x1036f0000 -        0x10373ffff libdispatch.dylib arm64e  <68af15bba53535629ed4f412272247ba> /usr/lib/system/introspection/libdispatch.dylib
       0x103794000 -        0x1037b7fff DebugHierarchyFoundation arm64e  <ac5bebfc476732359dd12862df05724c> /System/Library/PrivateFrameworks/DebugHierarchyFoundation.framework/DebugHierarchyFoundation
       0x10497c000 -        0x104987fff libobjc-trampolines.dylib arm64e  <4aba9420e4d03c989d62c653b259eab4> /private/preboot/Cryptexes/OS/usr/lib/libobjc-trampolines.dylib
               0x0 - 0xffffffffffffffff ??? unknown-arch  <00000000000000000000000000000000> ???
       0x1a5eb8000 -        0x1a608cfff Security arm64e  <946637c39834347e98c8f7219aadb9ef> /System/Library/Frameworks/Security.framework/Security
       0x1b323d000 -        0x1b4344fff WebKit arm64e  <5619324d2d0d322a84f6856484da9738> /System/Library/Frameworks/WebKit.framework/WebKit
       0x1b441c000 -        0x1b5c53fdf JavaScriptCore arm64e  <690b3848a0eb35b29ff6c347fbccff84> /System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore
       0x19d79d000 -        0x19dce0fff CoreFoundation arm64e  <0013a8b125243534b5ba681aaf18c798> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
       0x1eaab0000 -        0x1eaab8fff GraphicsServices arm64e  <d09b841af83c3dbcafc1ad9dab5d5fe1> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
       0x19ffc4000 -        0x1a1edefff UIKitCore arm64e  <404ac345125c3fe48e608952d037927b> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
       0x1a1edf000 -        0x1a31a8fff SwiftUI arm64e  <ee20cce9a10b3a05b39aaa7466b54873> /System/Library/Frameworks/SwiftUI.framework/SwiftUI
       0x1c3a76000 -        0x1c3af913f dyld arm64e  <c6ab7bed0bd23c6fa0213f8f22b57d1a> /usr/lib/dyld
       0x1eeeb3000 -        0x1eeeecff3 libsystem_kernel.dylib arm64e  <256c8b11269d34799221edc0e6284dac> /usr/lib/system/libsystem_kernel.dylib
       0x19c3bd000 -        0x19d0d1fff Foundation arm64e  <8e14980c8d863c93965ce3a1d761a45a> /System/Library/Frameworks/Foundation.framework/Foundation
       0x228504000 -        0x228510ff3 libsystem_pthread.dylib arm64e  <80ce8691d74734f2a720a2b76bf4148b> /usr/lib/system/libsystem_pthread.dylib
       0x1a55a6000 -        0x1a5625ffb libsystem_c.dylib arm64e  <4065a8811de03b378e7699600361e0d8> /usr/lib/system/libsystem_c.dylib
       0x19ec99000 -        0x19f05dfff CFNetwork arm64e  <5f93dbabb5b3379fbbef2320505615e3> /System/Library/Frameworks/CFNetwork.framework/CFNetwork
       0x228458000 -        0x22845fff3 libsystem_platform.dylib arm64e  <def0b18b9b0a3ed3a4876171c6d9b127> /usr/lib/system/libsystem_platform.dylib
       0x1a9091000 -        0x1a9113fff libswift_Concurrency.dylib arm64e  <01ad2d3e16d2374da565e3f1961013d4> /usr/lib/swift/libswift_Concurrency.dylib

EOF