NSPersistentStoreDeferredLightweightMigrationOptionKey
The key for enabling deferred lightweight migrations.
Declaration
let NSPersistentStoreDeferredLightweightMigrationOptionKey: StringDiscussion
As your managed object model changes, Core Data can use lightweight migrations to synchronize the underlying store data with those evolving entity definitions. These migrations happen at runtime, so they need to be fast or they can lead to a poor experience, because a migration must complete before your app can continue. Reduce the impact of migrations by deferring expensive cleanup tasks — such as dropping a table — until a more opportune time.
Deferred lightweight migrations are off by default. To enable them, add NSPersistentStoreDeferredLightweightMigrationOptionKey, with a value of true, to the options dictionary you provide when adding a persistent store to the coordinator.
let options = [
// Enable deferred lightweight migrations.
NSPersistentStoreDeferredLightweightMigrationOptionKey: true,
// Enable lightweight migrations.
NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true
]
let store = coordinator.addPersistentStore(
type: .sqlite,
at: storeURL,
options: options
)After you enable deferred lightweight migrations, Core Data continues to perform your lightweight migrations as usual, but defers any time-consuming cleanup tasks that don’t impact the execution of your app. Those tasks still need to run, but you choose when to run them. To determine whether there are deferred tasks to finish, query the store’s metadata with NSPersistentStoreDeferredLightweightMigrationOptionKey. If the returned value is true, execute those tasks using the coordinator. A single migration may defer several distinct tasks and you can execute them all at once using finishDeferredLightweightMigration(), or individually using finishDeferredLightweightMigrationTask().
let key = NSPersistentStoreDeferredLightweightMigrationOptionKey
if let hasMigration = store.metadata[key], hasMigration == true {
coordinator.finishDeferredLightweightMigration()
}