SwiftData crash on fetch

I have a strange crash which I have problems understanding. It only happens on a few devices, after a ModelContainer migration, and it doesn't seem to crash on the migration itself.

The fetch is done in onAppear, and shouldn't necessarily result in a crash, as it is an optional try:

let request = FetchDescriptor<Rifle>()
let data = try? modelContext.fetch(request)
if let data, !data.isEmpty {
    rifle = data.first(where: { $0.uuid.uuidString == settings.selectedRifleId }) ?? data.first!
}

When I get logs from users, there seems to be an error in encoding?

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

Triggered by Thread:  0

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libswiftCore.dylib            	       0x18e8bfd78 _assertionFailure(_:_:file:line:flags:) + 264
1   SwiftData                     	       0x24e18b480 0x24e14c000 + 259200
2   SwiftData                     	       0x24e193968 0x24e14c000 + 293224
3   SwiftData                     	       0x24e195a78 0x24e14c000 + 301688
4   libswiftCore.dylib            	       0x18e8e4084 _KeyedEncodingContainerBox.encodeNil<A>(forKey:) + 352
5   libswiftCore.dylib            	       0x18e8d79f0 KeyedEncodingContainer.encodeNil(forKey:) + 64
6   SwiftData                     	       0x24e19f09c 0x24e14c000 + 340124
7   SwiftData                     	       0x24e1a3dec 0x24e14c000 + 359916
8   libswiftCore.dylib            	       0x18ec10be8 dispatch thunk of Encodable.encode(to:) + 32
9   SwiftData                     	       0x24e1cd500 0x24e14c000 + 529664
10  SwiftData                     	       0x24e1cd0c8 0x24e14c000 + 528584
11  SwiftData                     	       0x24e1da960 0x24e14c000 + 584032
12  SwiftData                     	       0x24e1ee2ec 0x24e14c000 + 664300
13  SwiftData                     	       0x24e1d97d8 0x24e14c000 + 579544
14  SwiftData                     	       0x24e1eada0 0x24e14c000 + 650656
15  SwiftData                     	       0x24e1d989c 0x24e14c000 + 579740
16  SwiftData                     	       0x24e1eee78 0x24e14c000 + 667256
17  Impact                        	       0x1027403bc 0x10268c000 + 738236

I am facing the same issue as well. fetch crashes, but on the same FetchDescriptor fetchCount return correct result. To be more specific. I have a @ModelActor in which I am making some fetch and insert calls. This modelActor is initialized with modelContainer from the @Environment modelContext on .onAppear, and then calls to insert are made. After insertion is done, fetch call is made.

The @ModelActor is a singleton to maintain a single modelContext across the app. And I am also initializing the .modelContainer as a @State instead of a let.

The app works on iOS 17, but crashes just like OP mentioned on iOS 18.

I've checked the SwiftData forum posts but no solution, is there anything anyone from the Apple Dev Team can point to regarding this?

swiftData is very busted on iOS 18. It broke all of my preview with swiftData. Somehow persistentModelID no longer works, too

SwiftData crash on fetch
 
 
Q