I'm running a project with these settings:
Default Actor Isolation: MainActor Approachable Concurrency: Yes Strict Concurrency Checking: Complete (this issue does not appear on the other two modes)
I receive a warning for this very simple use case. Can I actually fix anything about this or is this a case of Core Data not being entirely ready for this?
In reference to this, there was a workaround listed in the release notes of iOS 26 beta 5 (https://forums.swift.org/t/defaultisolation-mainactor-and-core-data-background-tasks/80569/22). Does this still apply as the only fix for this?
This is a simplified sample meant to run on a background context. The issue obviously goes away if this function would just run on the MainActor, then I can remove the perform block entirely.
class DataHandler {
func createItem() async {
let context = ...
await context.perform {
let newGame = Item(context: context)
/// Main actor-isolated property 'timestamp' can not be mutated from a Sendable closure
newGame.timestamp = Date.now
// ...
}
}
}
The complete use case would be more like this:
nonisolated
struct DataHandler {
@concurrent
func saveItem() async throws {
let context = await PersistenceController.shared.container.newBackgroundContext()
try await context.perform {
let newGame = Item(context: context)
newGame.timestamp = Date.now
try context.save()
}
}
}
I confirm that the issue is still there in Xcode 26.1 Beta 2. When the Default Actor Isolation (SWIFT_DEFAULT_ACTOR_ISOLATION) is set to MainActor
, the compiler adds @MainActor
to the subclass of NSManagedObject
, which makes the attributes added to the subclass MainActor-isolated
, and triggers the warning.
Unless you'd change SWIFT_DEFAULT_ACTOR_ISOLATION
back to nonisolated
or live with the warning, the Swift forum post you provided mentions the following options:
-
Create a
nonisolated
variable to hold the closure, and call the closure from within theperform
method of the managed object context. -
Manually create the entity class (the subclass of
NSManagedObject
), and annotate it withnonisolated
.
These options work, but are not ideal. The best solution may be that Xcode annotates the entity class with nonisolated
when generating the code, but that needs to be implemented in Xcode. I'd hence suggest that you file a feedback report – If you do so, please share your report ID here.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.