SwiftData migration error: NSCloudKitMirroringDelegate are not reusable

Hello everyone,

I used SwiftData for v1 of an app and am now trying to make changes to the schema for v2. I created the v2 schema that adds a property to one of the models.

I need to populate the new property so I made a custom migration using didMigrate. However that doesn't seem to matter what I do in the migration because creating the ModelContainer throws an error before didMigrate ever gets called.

The error is:

Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Instances of NSCloudKitMirroringDelegate are not reusable and should have a lifecycle tied to a given instance of NSPersistentStore.}

Higher up in the Xcode output I see things like this (in order):

Request 'D25A8CB8-7341-4FA8-B2F8-3DE2D35B5273' was cancelled because the store was removed from the coordinator.
BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered
CloudKit setup failed because it couldn't register a handler for the export activity. There is another instance of this persistent store actively syncing with CloudKit in this process.

How can I know from this output what I am doing incorrectly?

Any idea what I should take a look at or try to do differently?

This is a simple app with three models and nothing fancy. The only change in the schema is to add a property. The new property is declared as optional and has an inverse that is also declared as optional.

Thanks for any insight!

A couple other things I noticed.

First, the crash occurs between the calls to willMigrate and didMigrate.

Second, if I try to run the app again after the crash then it runs fine, albeit with the new property being nil since didMigrate didn't execute, but this seems to suggest that the crash occurs downstream from the new schema version being applied to the database.

The error message indicates that your app had "another instance of this persistent store actively syncing with CloudKit," which is a wrong configuration when using SwiftData + CloudKit.

I am wondering if it was the migration process creating multiple persistent stores, or if it was your code unintentionally did that. The former will be a framework bug.

Do you have a minimal sample, with detailed steps, to reproduce the issue? If yes, I'd be interested in taking a look. I provided a code example demonstrating how to use SchemaMigrationPlan here, which you can use to create a sample by adding your model container configuration, if you would like to.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks Ziqiao, I will take a look at the code sample you pointed to and try to come up with a reduction that I can share. Either way I'll report back.

Trying different things with my project, I discovered that the problem only occurs when CloudKit syncing is enabled. Turning off syncing by passing cloudKitDatabase: .none to ModelConfiguration or by unchecking the CloudKit option in Signing & Capabilities makes the problem go away. But then of course iCloud syncing doesn't work. Makes sense considering the error message I get.

I was able to reproduce the problem easily with a new project based on the SwiftData Xcode template. I don't think it's doing anything out of the ordinary but I'm looking forward to find out.

The project can be found here: https://www.icloud.com/iclouddrive/0a5WkBaQRA1N-FOLGjKukV8Ag#MigrationSample

There are actually two projects, one for schema v1 and one for schema v2.

To reproduce the error I get:

  1. Open the MigrationSampleV1 project, build and run.
  2. In the running app, create some data.
  3. Open the MigrationSampleV2 project, build and run.

Expected result: The migration kicks in and the app opens.

Actual result: A crash occurs before the migration's didMigrate.

Oh here's a direct link to the zip file instead of a shared folder. https://www.icloud.com/iclouddrive/022QsLMXmVHAU2lAcuj4XuV4w#MigrationSample

Thanks for providing the project and steps. I tried them with my iPhone 16 Plus + iOS 18.4 beta 2 (22E5216h), and reproduced the issue.

Other than the error message you described, I saw other errors happening at the earlier stage. For the reference of other folks, I pasted the messages here:

CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1246): <NSCloudKitMirroringDelegate: 0x1361e8200>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x13624d900> (URL: file:///var/mobile/Containers/Data/Application/C3C44E20-5618-4837-BB14-21CFF9E45799/Library/Application%20Support/default.store)
Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=The mirroring delegate could not initialize because it's store was removed from the coordinator.}

...

BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered (com.apple.coredata.cloudkit.activity.export.E2260BCD-9E87-4C0E-B3F3-68CA9843D246).
CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:](610): <NSCloudKitMirroringDelegate: 0x1361e8500> - resetting internal state after error: Error Domain=NSCocoaErrorDomain Code=134422 "CloudKit setup failed because it couldn't register a handler for the export activity. There is another instance of this persistent store actively syncing with CloudKit in this process." UserInfo={NSUnderlyingException=Illegal attempt to register a second handler for activity identifier com.apple.coredata.cloudkit.activity.export.E2260BCD-9E87-4C0E-B3F3-68CA9843D246, NSURL=file:///var/mobile/Containers/Data/Application/C3C44E20-5618-4837-BB14-21CFF9E45799/Library/Application%20Support/default.store, activityIdentifier=com.apple.coredata.cloudkit.activity.export.E2260BCD-9E87-4C0E-B3F3-68CA9843D246, NSLocalizedFailureReason=CloudKit setup failed because it couldn't register a handler for the export activity. There is another instance of this persistent store actively syncing with CloudKit in this process.}

...

CoreData: error: Store failed to load.  <NSPersistentStoreDescription: 0x136028ea0> (type: SQLite, url: file:///var/mobile/Containers/Data/Application/C3C44E20-5618-4837-BB14-21CFF9E45799/Library/Application%20Support/default.store) with error = Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Instances of NSCloudKitMirroringDelegate are not reusable and should have a lifecycle tied to a given instance of NSPersistentStore.} with userInfo {
    NSLocalizedFailureReason = "Instances of NSCloudKitMirroringDelegate are not reusable and should have a lifecycle tied to a given instance of NSPersistentStore.";
}

I believe this is a system bug – It is actually quite similar to a known issue (r.127136484) that is supposed to be fixed. Now that the issue appears in today's latest beta, I’d suggest that you file a feedback report – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks Ziqiao. I filed the report. It's FB16712454.

SwiftData migration error: NSCloudKitMirroringDelegate are not reusable
 
 
Q