NSPersistentStoreCoordinator
An object that enables an app’s contexts and the underlying persistent stores to work together.
Declaration
class NSPersistentStoreCoordinatorMentioned in
Overview
A managed object context uses a coordinator to facilitate the persistence of its entities in the coordinator’s registered stores. A context can’t function without a coordinator because it relies on the coordinator’s access to the managed object model. The coordinator presents its registered stores as an aggregate, allowing a context to operate on the union of those stores instead of on each individually. A coordinator performs its work on a private queue and executes that work serially. You can use multiple coordinators if the work requires separate queues.
Use a coordinator to add or remove persistent stores, change the type or location on-disk of those stores, query the metadata of a specific store, defer a store’s migrations, determine whether two objects originate from the same store, and so on.
Topics
Creating a persistent store coordinator
Managing configuration
Registering store types
Adding or removing a store
addPersistentStore(type:configuration:at:options:)addPersistentStore(ofType:configurationName:at:options:)addPersistentStore(with:completionHandler:)remove(_:)
Modifying a store
destroyPersistentStore(at:type:options:)migratePersistentStore(_:to:options:type:)replacePersistentStore(at:destinationOptions:withPersistentStoreFrom:sourceOptions:type:)destroyPersistentStore(at:ofType:options:)migratePersistentStore(_:to:options:withType:)replacePersistentStore(at:destinationOptions:withPersistentStoreFrom:sourceOptions:ofType:)
Managing a store’s location
Managing a store’s metadata
setMetadata(_:type:at:options:)metadataForPersistentStore(type:at:options:)setMetadata(_:forPersistentStoreOfType:at:options:)metadataForPersistentStore(ofType:at:options:)metadata(for:)setMetadata(_:for:)NSStoreTypeKeyNSStoreUUIDKey
Deferring a store’s migrations
NSPersistentStoreDeferredLightweightMigrationOptionKeyfinishDeferredLightweightMigrationTask()finishDeferredLightweightMigration()
Performing tasks
Maintaining a record of changes
Integrating with Spotlight
NSCoreDataCoreSpotlightExporterNSCoreDataCoreSpotlightDelegateSpotlight record keysShowcase App Data in Spotlight
Getting individual object identifiers
Responding to changes of the coordinator’s registered stores
NSPersistentStoreCoordinatorStoresWillChangeNSPersistentStoreCoordinatorStoresDidChangeNSPersistentStoreCoordinatorWillRemoveStoreNotification keys