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).