Using Core Data with the Swift 6 language mode

I'm starting to work on updating my code for Swift 6. I have a number of pieces of code that look like this:

private func updateModel() async throws {
    try await context.perform { [weak self] in
        // do some work
    }
}

After turning on strict concurrency checking, I get warnings on blocks like that saying "Sending 'self.context' risks causing data races; this is an error in the Swift 6 language mode."

What's the best way for me to update this Core Data code to work with Swift 6?

Yeah, the warning is triggered because NSManagedObjectContext is not a Sendable type. Does updateModel need to be isolated to a certain actor? If not, you can consider annotating updateModel with nonisolated, which should calm down the warning.

If updateModel does rely on other isolated and non-sendable variables, check if you can convert them to Sendable types.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks! I'm wondering if you have any advice about making an NSManagedObject subclass Sendable? I'm not really sure how I'd go about it.

Using Core Data with the Swift 6 language mode
 
 
Q