Crash when using SwiftData Access in background triggered from WCSessionDelegate

I'm having a weird issue. I have a watch app which communicates with the phone using a WCSessionDelegate.

if the phone app is open everything works fine, but if the app is closed, when the watch sends a message, my app is woken from the background, after which onAppear() is called in my app which causes a swiftData query to run.

Calling any swiftdata function from a backgrounded app causes it to immediately crash with the following stack trace. Any ideas what im doing wrong? or a better way to trigger my code instead of onAppear, so it won't be called when my watch wakes my app from the background?

.onAppear {
     reloadData()
 }



  private func reloadData() {
        let fetch = FetchDescriptor<SavedServer>(
            predicate: nil,
            sortBy: [.init(\.displayOrder)]
        )
        guard let results = try? modelContext.fetch(fetch) else {
            self.rows = []
            return
        }
       self.rows = results
}
-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: 057999AF-7840-410E-B3EE-29082C5AED00
CrashReporter Key:   28AF2AA0-4626-9964-9664-36077DAF4E1A
Hardware Model:      MacBookPro18,2
Process:             MC Status [68915]
Path:                /Users/USER/Library/Developer/CoreSimulator/Devices/C61698BA-C4CA-4DD9-B824-DBF57AC65090/data/Containers/Bundle/Application/A685371C-9174-4CF7-9E99-D573310CC3E5/MC Status.app/MC Status
Identifier:          com.shemeshapps.MinecraftServerStatus
Version:             2.0 (1)
Code Type:           ARM-64 (Native)
Role:                Non UI
Parent Process:      launchd_sim [55432]
Coalition:           com.apple.CoreSimulator.SimDevice.C61698BA-C4CA-4DD9-B824-DBF57AC65090 [164301]
Responsible Process: SimulatorTrampoline [53834]

OS Version:          macOS 13.4.1 (22F82)
Release Type:        User
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                	       0x18046589c __exceptionPreprocess + 160
1   libobjc.A.dylib               	       0x18005c09c objc_exception_throw + 56
2   CoreData                      	       0x184989b94 -[NSFetchRequest(_NSInternalMethods) _incrementInUseCounter] + 0
3   CoreData                      	       0x1849aa99c -[NSManagedObjectContext executeRequest:error:] + 164
4   CoreData                      	       0x1848fe250 NSManagedObjectContext.fetch<A>(_:) + 80
5   SwiftData                     	       0x1a89b7ad8 ModelContext.fetch<A>(_:) + 124
6   SwiftData                     	       0x1a89c48c0 dispatch thunk of ModelContext.fetch<A>(_:) + 20
7   MC Status                     	       0x102530ef4 MainAppContentView.reloadData(forceRefresh:) + 752 (MainAppContentView.swift:112)
8   MC Status                     	       0x102533540 closure #2 in MainAppContentView.body.getter + 44 (MainAppContentView.swift:78)
9   SwiftUI                       	       0x108a0b7a0 0x107b8c000 + 15202208
10  SwiftUI                       	       0x108a0b7bc 0x107b8c000 + 15202236
11  SwiftUI                       	       0x108a0b7a0 0x107b8c000 + 15202208
12  SwiftUI                       	       0x108fdce70 0x107b8c000 + 21302896
13  SwiftUI                       	       0x108fd6ec0 0x107b8c000 + 21278400
14  SwiftUI                       	       0x1081edb24 0x107b8c000 + 6691620
15  SwiftUI                       	       0x10928d650 0x107b8c000 + 24122960
16  libdispatch.dylib             	       0x1801424f4 _dispatch_call_block_and_release + 24
17  libdispatch.dylib             	       0x180143d3c _dispatch_client_callout + 16
18  libdispatch.dylib             	       0x180152b24 _dispatch_main_queue_drain + 1272
19  libdispatch.dylib             	       0x18015261c _dispatch_main_queue_callback_4CF + 40
20  CoreFoundation                	       0x1803c61b4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
21  CoreFoundation                	       0x1803c08cc __CFRunLoopRun + 1936
22  CoreFoundation                	       0x1803bfd28 CFRunLoopRunSpecific + 572
23  GraphicsServices              	       0x189864bc0 GSEventRunModal + 160
24  UIKitCore                     	       0x103b30208 -[UIApplication _run] + 868
25  UIKitCore                     	       0x103b33e80 UIApplicationMain + 124
26  SwiftUI                       	       0x108a10524 0x107b8c000 + 15222052
27  SwiftUI                       	       0x108a103c4 0x107b8c000 + 15221700
28  SwiftUI                       	       0x108722088 0x107b8c000 + 12148872
29  MC Status                     	       0x102506d30 static MCStatusApp.$main() + 40
30  MC Status                     	       0x102506de0 main + 12 (MCStatusApp.swift:12)
31  dyld_sim                      	       0x1028fd558 start_sim + 20
32  dyld                          	       0x1026b1f28 start + 2236
33  ???                           	0x3c15800000000000 ???


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	       0x102f1cfa8 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x10285712c pthread_kill + 256
2   libsystem_c.dylib             	       0x1801375ec abort + 104
3   libc++abi.dylib               	       0x180263c78 abort_message + 128
4   libc++abi.dylib               	       0x180255198 demangling_terminate_handler() + 300
5   libobjc.A.dylib               	       0x180037bf0 _objc_terminate() + 124
6   libc++abi.dylib               	       0x180263150 std::__terminate(void (*)()) + 12
7   libc++abi.dylib               	       0x180263100 std::terminate() + 52
8   libdispatch.dylib             	       0x180143d50 _dispatch_client_callout + 36
9   libdispatch.dylib             	       0x180152b24 _dispatch_main_queue_drain + 1272
10  libdispatch.dylib             	       0x18015261c _dispatch_main_queue_callback_4CF + 40
11  CoreFoundation                	       0x1803c61b4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
12  CoreFoundation                	       0x1803c08cc __CFRunLoopRun + 1936
13  CoreFoundation                	       0x1803bfd28 CFRunLoopRunSpecific + 572
14  GraphicsServices              	       0x189864bc0 GSEventRunModal + 160
15  UIKitCore                     	       0x103b30208 -[UIApplication _run] + 868
16  UIKitCore                     	       0x103b33e80 UIApplicationMain + 124
17  SwiftUI                       	       0x108a10524 0x107b8c000 + 15222052
18  SwiftUI                       	       0x108a103c4 0x107b8c000 + 15221700
19  SwiftUI                       	       0x108722088 0x107b8c000 + 12148872
20  MC Status                     	       0x102506d30 static MCStatusApp.$main() + 40
21  MC Status                     	       0x102506de0 main + 12 (MCStatusApp.swift:12)
22  dyld_sim                      	       0x1028fd558 start_sim + 20
23  dyld                          	       0x1026b1f28 start + 2236

I've had the same exact issue for a while now. I believe the ModelContext is not loaded in a background context because it is an Environment variable. I have no idea what the expected fix is here, to be completely honest.

Crash when using SwiftData Access in background triggered from WCSessionDelegate
 
 
Q