Apologies for the delay. It took me some digging, but I have resolved the issue. In the end it had nothing to do with SwiftData, but rather with Actors and illegally crossing actor boundaries.
I have a singleton that holds a reference to my main SwiftData Container, that I can access using SwiftDataCoordinator.shared.container
This shared instance lives on the MainActor.
My issue arose because I was instantiating my ModelActors like this:
Task.detached {
let modelActor = SortableDataService(modelContainer: SwiftDataCoordinator.shared.container)
etc...
The fix was to do this:
let container = SwiftDataCoordinator.shared.container
Task.detached {
let modelActor = SortableDataService(modelContainer: container)
etc...
In the end quite obvious, but it took me a while to see it.
Leaving this here in case others run into this.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: