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.

SwiftData migration error: NSCloudKitMirroringDelegate are not reusable
 
 
Q