I had the same issue when trying to remove a child object from a relationship directly using the context.delete method
I fixed my crash by removing the object from the parent object list and saving the context.
So instead of doing
mainModelContext.delete(child)
I am doing
// get the child index from the parent
parent.childs.remove(at: childIndex)
mainModelContext.save()
This seems to fix the crash and its also trigger update on the parent model if a view is observing it.
My experience with SwiftData so far has been that if you have to modify relationships it's better to do it from parent side. I have encountered crashes or updates not triggering as expected otherwise.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: