Instances of NSPersistentStoreCoordinator associate persistent stores (by type) with a model (or more accurately, a configuration of a model) and serve to mediate between the persistent store or stores and the managed object context or contexts. Instances of NSManagedObjectContext use a coordinator to save object graphs to persistent storage and to retrieve model information. A context without a coordinator is not fully functional as it cannot access a model except through a coordinator. The coordinator is designed to present a façade to the managed object contexts such that a group of persistent stores appears as an aggregate store. A managed object context can then create an object graph based on the union of all the data stores the coordinator covers.


Coordinators do the opposite of providing for concurrency—€”they serialize operations. If you want to use multiple threads for different write operations you use multiple coordinators. Note that if multiple threads work directly with a coordinator, they need to lock and unlock it explicitly.

Each coordinator (and thus container) may use different copies, and hence different versions, of a managed object model. This allows you to cleanly deal with file versioning.

The coordinator gives access to its underlying object stores. You can retrieve an object store when you first add one (using addPersistentStore(ofType:configurationName:at:options:)), or by using persistentStore(for:) or persistentStores. This allows you to to determine, for example, whether a store has already been added, or whether two objects come from the same store.

For more details about these tasks, see Using Persistent Stores in Core Data Programming Guide.


Registered Store Types

class func registerStoreClass(AnyClass, forStoreType: String)

Registers a given NSPersistentStore subclass for a given store type string.

Initializing a Coordinator

init(managedObjectModel: NSManagedObjectModel)

Initializes the receiver with a managed object model.

var managedObjectModel: NSManagedObjectModel

The receiver’s managed object model.

Configuring Persistent Stores

func addPersistentStore(ofType: String, configurationName: String?, at: URL?, options: [AnyHashable : Any]? = nil)

Adds a new persistent store of a specified type at a given location, and returns the new store.

func setURL(URL, for: NSPersistentStore)

Sets the URL for a given persistent store.

func remove(NSPersistentStore)

Removes a given persistent store.

func migratePersistentStore(NSPersistentStore, to: URL, options: [AnyHashable : Any]? = nil, withType: String)

Moves a persistent store to a new location, changing the storage type if necessary.

var persistentStores: [NSPersistentStore]

The persistent stores associated with the receiver.

func persistentStore(for: URL)

Returns the persistent store for the specified URL.

func url(for: NSPersistentStore)

Returns the URL for a given persistent store.

Managing Stores in iCloud

class func removeUbiquitousContentAndPersistentStore(at: URL, options: [AnyHashable : Any]? = nil)

Deletes all ubiquitous content for all peers for the persistent store at a given URL and also delete the local store file.


Executing a Fetch Request

func execute(NSPersistentStoreRequest, with: NSManagedObjectContext)

Sends a request to all the persistent stores associated with the receiver.


func lock()

Attempts to acquire a lock.

func tryLock()

Attempts to acquire a lock.

func unlock()

Relinquishes a previously acquired lock.


Working with Metadata

func metadata(for: NSPersistentStore)

Returns a dictionary that contains the metadata currently stored or to-be-stored in a given persistent store.

func setMetadata([String : Any]?, for: NSPersistentStore)

Sets the metadata stored in the persistent store during the next save operation executed on it to metadata.

class func metadataForPersistentStore(ofType: String?, at: URL)

Returns a dictionary containing the metadata stored in the persistent store at a given URL.


Working with Spotlight External Records

class func elementsDerived(fromExternalRecordAt: URL)

Returns a dictionary containing the parsed elements derived from the Spotlight external record file specified by the given URL.

Discovering Object IDs

func managedObjectID(forURIRepresentation: URL)

Returns an object ID for the specified URI representation of an object ID if a matching store is available, or nil if a matching store cannot be found.


Store Types

Types of persistent store.

Store Metadata

Keys used in a store’€™s metadata dictionary.

Stores Change Notification User Info Keys

An NSPersistentStoreCoordinatorStoresDidChange notification is posted whenever persistent stores are added to or removed from a persistent store coordinator, or when store UUIDs change. The userInfo dictionary contains information about the stores that were added or removed using these keys.

Migration Options

Migration options, specified in the dictionary of options when adding a persistent store using addPersistentStore(ofType:configurationName:at:options:).

Versioning Support

Keys in store metadata to support versioning.


These constants are used as the value corresponding to the NSPersistentStoreUbiquitousTransitionTypeKey in the user info dictionary of NSPersistentStoreCoordinatorStoresWillChange and NSPersistentStoreCoordinatorStoresDidChange notifications to identify the type of event leading to a change.

Spotlight External Record File Format Options

Constants that specify the format for Spotlight external records. These constants are the possible values for the key NSPersistentStoreFileProtectionKey.

Spotlight External Record Elements

Keys for the dictionary with the parsed elements derived from Spotlight external record file.


static let NSPersistentStoreCoordinatorStoresDidChange: NSNotification.Name

Posted whenever persistent stores are added to or removed from a persistent store coordinator, or when store UUIDs change.

static let NSPersistentStoreCoordinatorStoresWillChange: NSNotification.Name

Posted before the list of open persistent stores changes.

static let NSPersistentStoreCoordinatorWillRemoveStore: NSNotification.Name

Posted whenever a persistent store is removed from a persistent store coordinator.

static let NSPersistentStoreDidImportUbiquitousContentChanges: NSNotification.Name

Posted after records are imported from the ubiquitous content store.


Instance Properties


Inherits From