I don't understand why Example 1 fails to compile and Example 2 compiles just fine in swift 6?
Note in the following examples that gDataController.container
is an NSPersistentContainer
Example 1:
try await gDataController.container.performBackgroundTask { bkContext in
try bkContext.save()
}
The Example 1 error is:
Sending value of non-Sendable type '(NSManagedObjectContext) throws -> ()' risks causing data races
However Example 2 compiles just fine:
Example 2:
let bkContext = gDataController.container.newBackgroundContext()
try bkContext.performAndWait {
try bkContext.save()
}
It would appear to me that these are exactly opposite of my expectations. Clearly in Example 2, bkContext
is being passed into the closure.
Can anyone explain this?