I have a Core Data app in production and would like to add attributes to existing entities. After research, I found that this is a "lightweight migration," and just need to add NSMigratePersistentStoresAutomaticallyOption
and NSInferMappingModelAutomaticallyOption
options to my persistentStoreDescriptions
before loading the persistent store:
let container = NSPersistentCloudKitContainer(name: "app")
guard let description = container.persistentStoreDescriptions.first else { fatalError("Container not available") }
//For migration VVV
description.setOption(true as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption)
description.setOption(true as NSNumber, forKey: NSInferMappingModelAutomaticallyOption)
//End of migration additions ^^^
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Store loading failure")
}
}
While this seems like an easy fix, I just have two questions that I couldn't find answers to anywhere else:
1. Can I leave these two new migration options in for all future versions of the app for anyone who updates their app version weeks from now?
2. Since I use an NSPersistentCloudKitContainer
, when releasing the new data model to the CloudKit Dashboard, should users' current versions of their app include the two migration options? I'm afraid if the CloudKit data model is different from their versions of the app, it will crash their apps when syncing their items
Any help on this is greatly appreciated! It's always a scary experience changing the data model, and I have never done it right and always resulted in crashing people's apps. I really appreciate any help you can provide.
There are set to true
by default. Unless you have turned them to false
at some point you won't see any difference