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 attributes. The model file is represented as an instance of NSManagedObjectModel.
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, a specific store is added to the coordinator. At a minimum, this store needs to know its type and where it should persist the data.
The portal through which you interact with your entities is the NSManagedObjectContext. The managed object context is associated with a specific persistent store coordinator. As we said earlier in this chapter, 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.