SwiftData/ModelSnapshot.swift:46: Fatal error: A ModelSnapshot must be initialized with a known-keys dictionary

I'm running into a crash when trying to delete an item from a list that's loaded using SwiftData. The app works fine when selecting or displaying the data, but the moment I confirm a deletion, it crashes with this error:

SwiftData/ModelSnapshot.swift:46: Fatal error: A ModelSnapshot must be initialized with a known-keys dictionary

This happens right after I delete an item from the list using modelContext.delete(). I’ve double-checked that the item exists and is valid, and I'm not sure what I'm doing wrong. The data is loaded using @Query and everything seems normal until deletion.

For further information, I have tried this on a new IOS project where I have one super Model class with a cascading relationship on a child class. When trying to delete the parent class while connected to one or more children, it still gives me the error.

The same thing is happening with my original project. Class A has a relationship (cascading) with Class B. Attempting to delete Class A while there are relationships with Class B throws this error.

If anyone has experienced this or knows what causes it, please let me know. I’m not even sure where to start debugging this one.

Thanks in advance!

I tried to reproduce on a iOS 18.4 large but failed, which isn't that surprising given how little information you have provided.

Could you add enough context and code so we can reproduce this issue?

I have the same issue in my app, which a user pointed out to me yesterday.

However, in my case, the crash only occurs when a widget on the Home Screen is "using" the SwiftData model object I'm trying to delete.

Does your app use widgets, out of interest?

SwiftData/ModelSnapshot.swift:46: Fatal error: A ModelSnapshot must be initialized with a known-keys dictionary
 
 
Q