For the More Curious: The Core Data Stack

NSManagedObjectModel

You worked with the model file earlier in the chapter. The model file is where you define the entities for your application along with their properties. The model file is an instance of NSManagedObjectModel.

NSPersistentStoreCoordinator

Core Data can persist data in several formats:

SQLite

Data is saved to disk using a SQLite database. This is the most commonly used store type.

Atomic

Data is saved to disk using a binary format.

XML

Data is saved to disk using an XML format. This store type is not available on iOS.

In-Memory

Data is not saved to disk, but instead is stored in memory.

The mapping between an object graph and the persistent store is accomplished using an instance of NSPersistentStoreCoordinator. The persistent store coordinator needs to know two things: What are my entities? and, Where am I saving to and loading data from? To answer these questions, you instantiate an NSPersistentStoreCoordinator with the NSManagedObjectModel. Then you add a persistent store, representing one of the persistence formats above, to the coordinator.

After the coordinator is created, you attempt to add a specific store to the coordinator. At a minimum, this store needs to know its type and where it should persist the data.

NSManagedObjectContext

The portal through which you interact with your entities is the NSManagedObjectContext. The managed object context is associated with a specific persistent store coordinator. You can think of the managed object context as an intelligent scratch pad. When you ask the context to fetch some entities, the context will work with its persistent store coordinator to bring temporary copies of the entities and object graph into memory. Unless you ask the context to save its changes, the persisted data remains the same.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset