Post

Replies

Boosts

Views

Activity

Reply to SwiftData does not work on a background Task even inside a custom ModelActor.
Using Task.detached { ... } works for me. I'm seeing the model actor methods being run on a background thread: @ModelActor actor CustomModelActor { func updateObjects(...) { // This will run on a background thread } } struct MainView: View { @Environment(\.modelContext) var modelContext var body: some View { MyView() .task { let container = modelContext.container Task.detached { let modelActor = CustomModelActor(modelContainer: container) try await modelActor.updateObjects(...) } } } } Note that you need to create CustomModelActor in the detached Task as well, otherwise it will be bound to the thread it was created on (which in this case would be the main thread).
Oct ’23