It seems no method to get the mainContext injected in environment directly, and the context is not sendable, you can not use it on other actor/thread. But you can get the context from your model like model.context.
actor BackgroundActor: ModelActor {
nonisolated public let executor: any ModelExecutor
init(container: ModelContainer) {
let context = ModelContext(container)
executor = DefaultModelExecutor(context: context)
}
func run(task: (ModelContext) -> Void) {
task(context)
}
}
You can use this wrapper like this:
databaseActor.run { context in
// ....
}