iCloud Core Data Model Schema Migration

2019-08-07 15:46发布

问题:

Acording to Apple's Document, The lightweight migration for core data connected with iCloud can be performed automatically. And another documents from Apple says that:

If you migrate a persistent store configured with a NSPersistentStoreUbiquitousContentNameKey option to a new model version, the store’s history of changes originating from the current device will also be migrated and then merged with any other devices configured with that new model version. Any changes from stores using the new version are also merged in.

Existing changes can not, however, be migrated to a new model version if the migration is performed using a custom mapping model.

I could change my app to use iCloud, and I could migrate(manual) local database into ubiquity one successfully. So first core data with iCloud doesn't show any problems.

However, when I added new model version, The migrated store contains nothing. It is supposed to be contains migrated content that is created with older version of model. the store’s history of changes seems to be ignored for some reason.

As a result, Testers who installed new version of the app loses content which created with earlier version. There were no logs or errors.

When I tried same initializing process to store without NSPersistentStoreUbiquitousContentNameKey option(local store), everything were fine. All content are migrated and contained by new store correctly.

And the second documents says:

Existing changes can not, however, be migrated to a new model version if the migration is performed using a custom mapping model.

I think that My needs can be match this case. I have also custom mapping models. But I don't know the place to trigger.

My store options are very typical, not so special:

{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSPersistentStoreUbiquitousContentNameKey = "moment-cloud";
    NSPersistentStoreUbiquitousContentURLKey = Logs;
}

Store URL is created as:

NSURL* docURL = [fileManager URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask].firstObject;
return [docURL URLByAppendingPathComponent: kFileNameDatabaseForUbiquitous];