SwiftData: Unexpected backing data for snapshot creation

When deleting a SwiftData entity, I sometimes encounter the following error in a document based SwiftUI app:

Fatal error: Unexpected backing data for snapshot creation: SwiftData._FullFutureBackingData<MyEntityClass>

The deletion happens in a SwiftUI View and the code used to retrieve the entity is standard (the ModelContext is injected from the @Environment):

let myEntity = modelContext.model(for: entityIdToDelete)
modelContext.delete(myEntity)

Unfortunately, I haven't yet managed to isolate this any further in order to come up with a reproducible PoC.

Could you give me further information about what this error means?

My guess is that a view is trying to access an instance of the object after it has been removed.

I had that guess too, however, myEntity.isDeleted is false when this occurs.

Also, I am fetching the model right before deleting it. Here I have just noticed something weird in the documentation, namely that model(for:) creates an unsaved instance if called with an unknown ID. So I tried the same with registeredModel(for:), but I get the same error

https://developer.apple.com/documentation/swiftdata/modelcontext/model(for:)

The error happens when SwiftData tries to create a snapshot for a model and the backing data doesn't have the right type, which ... doesn't quite explain anything...

What system version do you use to see the issue? Did you ever try with the current latest public release (26.0.2) and Beta (26.1 Beta 4)? If you find a reproducible case and don't mind to share, I may be able to take a closer look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData: Unexpected backing data for snapshot creation
 
 
Q