I'm not familiar with CoreData. There was a thread about a special behavior of ModelActor in this thread, though that behavior is invisible to type system so I guess it's irrelevant. That said, person is created inside the closure so it can be accessed synchronously of course. I'm glad the sending hack works for you.
FWIW, I haven't been able to reproduce the issue using regular actors. The following code produces diagnostic rather than compiles with misbehavior. This is the same issue as demonstrated by isolatedToGlobalActor in another thread.
actor DBActor {
func run(_ work: nonisolated(nonsending) () async -> Void) async {
await work()
}
}
class DBObject {
func modify() {}
func save() {}
}
// Note: the code compiles if `buttonPushed` is nonisolated.
@MainActor func buttonPushed() {
let myDatabaseActor = DBActor()
Task {
await myDatabaseActor.run { // error: sending value of non-Sendable type '() async -> Void' risks causing data races [#RegionIsolation::SendingRisksDataRace]
print("\(#isolation as (any Actor)?)")
let dbObject = DBObject()
dbObject.modify()
dbObject.save()
}
}
}