NSPersistent 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
NSManaged 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.
- iOS 3.0+
- macOS 10.4+
- tvOS 3.0+
- watchOS 2.0+
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
add), or by using
persistent. This allows you to to determine, for example, whether a store has already been added, or whether two objects come from the same store.
You move a store from one location to another, or change the type of a store, using
Persistent Store(_: to: options: with Type:)
You can set metadata for a given store using the persistent store coordinator (
For more details about these tasks, see Using Persistent Stores in Core Data Programming Guide.