Core Data Features
Core Data framework provides a simple way of maintaining the life cycle of the objects and object graph management including persistence. Here are some of the main features of Core Data
- Relationship maintenance
- Core Data manage change propagation and maintain consistency of relationships among objects
- Provides mechanism for schema migration.
- Automatic support for storing objects in external data repositories
- Sophisticated merge policies and conflict resolution
Core Data Terminologies
Managed Object Context
We can think of managed object context as a staging area for all the objects where modifications to the objects happen. The data store in the managed object context is not persisted in the persistent store.
We need to explicitly call the save on the context to persist the data.
Model objects that tie into in the Core Data framework are known as managed objects. All managed objects must be registered with a managed object context. You add objects to the graph and remove objects from the graph using the context.
To retrieve data using a managed object context, you create a fetch request. A fetch request has three parts.
* It should what type of entity data it should fetch
* It may also contain a predicate object that specifies conditions that objects must match
* An array of sort descriptor objects that specifies the order in which the objects should appear. Like you can specify that you want the latest created objects to appear first or sort by a name parameter withing the model
A given persistent object store is associated with a single file or other external data store and is ultimately responsible for mapping between data in that store and corresponding objects in a managed object context.
If you are using core data with UITableView then this is the most useful class for that purpose. You use a fetched results controller to efficiently manage the results returned from a Core Data fetch request to provide data for a UITableView object.
You configure a fetch results controller using a fetch request that specifies the entity, an array containing at least one sort ordering, and optionally a filter predicate. The fetched results controller efficiently analyzes the result of the fetch request and computes all the information about sections in the result set. It also computes all the information for the index based on the result set.
In addition, fetched results controllers provide the following features:
Optionally monitor changes to objects in the associated managed object context and report changes in the results set to its delegate. We need to implement NSFetchedResultsControllerDelegate if we want to be notified when there are changes to the managed object context.
If you get an error like this: Unable to load class named ‘Menu’ for the entity ‘Menu’. Class not found, using default NSManagedObject instead.
Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the NSManagedObject class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.