Question

I am trying to avoid ever upgrading a Core Data store using lightweight migration while it is shared in iCloud so I want to migrate the store to a local only store and then perform the upgrade using automatic lightweight migration. Doing so while the store is in iCloud does not always work on OSX and seems a touch fragile on iOS, takes a long time, lots of stores get swapped out from under the App, etc..

Here is the problem I am trying to solve: - on launch detect when an App needs to upgrade its Core Data store to a new model version - if an upgrade is required and the store is shared in iCloud then migrate the store to a local store without Core Data automatically performing a lightweight migration (upgrade) - then open the store and perform the upgrade while the store is local

So my solution currently, which works, is currently to do the following: - create two Core Data model bundles in the App bundle, both identical but one set to use the latest version of the model and the other the earlier version. Called, for example, 'App_model' and 'App_model_old'. - using the [NSManagedObjectModel isConfiguration:compatibleWithStoreMetadata:] API check for compatibility using 'App_model' and stores using older versions of the model should return NO. - now migrate the store to a local store using the migratePersistentStore API and using the old version of the model, 'App_model_old' when adding the store to the storeCoordinator to ensure no lightweight migration takes place. - then open the store using the new model bundle 'App_model' so that Core Data performs lightweight migration - then finally migrate the store back to ICloud.

This seems to work fine using the two Core Data bundles but its just an extra hassle to have to keep cloning the model bundles (not much mind you). Nevertheless I was wondering if anyone knows if it is possible to programatically specify which model version to use in the Core Data model bundle to avoid having to create two bundles or alternately to prevent the lightweight migration from happening but still allow the migratePersistentStore API to be used to move the store.

No correct solution

OTHER TIPS

Oops - I think I just found the answer in Apple docs here https://developer.apple.com/library/mac/documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmCustomizing.html#//apple_ref/doc/uid/TP40004399-CH8-SW1

NSDictionary *sourceMetadata =
    [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:sourceStoreType
                                  URL:sourceStoreURL
                                  error:&error];
NSManagedObjectModel *sourceModel =
    [NSManagedObjectModel mergedModelFromBundles:nil
                            forStoreMetadata:sourceMetadata];

Why not just pass no for NSMigratePersistentStoresAutomaticallyOption? Won't that disable lightweight migration?

NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption: @NO };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top