Core Data is a framework by apple which deals with the model layer of the Model-View-Controller design pattern in the application. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It provides the optimized solution for saving the objects in to the memory and fetching it from the storage when needed. Core Data is used as the in memory store for the application as it provides generalized and automated solutions to common tasks associated with object life-cycle and object graph management, including persistence on disk.

It takes advantage of information provided in the model and runtime features not typically employed in application-level code. Moreover, in addition to providing excellent security and error-handling, it offers best memory scalability of any competing solution.

Core Data Framework is made up of core data stack . It is the ordered collection of objects from a managed object context, through a persistent object store coordinator, to a persistent store or collection of persistent stores. A stack is effectively defined by a persistent store coordinator ,there is one and only one per stack. Creating a new persistent store coordinator implies creating a new stack.


Fig:1.0_ Core Data Stack Contents.

In Core Data the NSManagedObjectModel, the NSPersistentStoreCoordinator, and the NSManagedObjectContext all of these on the stack work together to allow a program to retrieve and store NSManagedObject instances. In most situations, the program will access the NSManagedObjectContext only once the stack has been created. It is possible to access the other components of the stack, but it is rarely necessary.

Managed Object : Model objects that tie into in the Core Data framework are known as managed objects .It is an instance of NSManagedObject or a subclass of NSManagedObject. After creation it should be registered with a managed object context.

Managed Object Context : When the objects are fetched from the persistent store, they are brought in to managed object context where they form an object graph (or collection of object graphs). You can then modify those objects however you like. Unless you actually save those changes, however, the persistent store remains unaltered. It is an instance of NSManagedObjectContext. An NSManagedObjectContext object represents a single “object space” or scratch pad in an application. The context is a powerful object with a central role in the life-cycle of managed objects, with responsibilities from life-cycle management (including faulting) to validation, inverse relationship handling, and undo/redo.

Managed Object Model : An object that is an instance of NSManagedObjectModel. An NSManagedObjectModel object describes a schema, a collection of entities (data models) that you use in your application. It uses a private internal store to maintain its properties and implements all the basic behavior required of a managed object.

Img1Fig:1.1_Managed Object Model

Persistent Object Store Coordinator : An object that is an instance of NSPersistentStoreCoordinator. A coordinator associates persistent stores and a configuration of a managed object model and presents a facade to managed object contexts such that a group of persistent stores appears as a single aggregate store.

Persistent Object Store : A repository in which objects may be stored. A repository is typically a file, which may be XML, binary, or a SQL database. The store format is transparent to the application. Core Data also provides an in-memory store that lasts no longer than the lifetime of a process.


Fig:1.2_Advance Persistence Stack

About Persistent Documents :

We can create and configure the persistence stack programmatically. In many cases, when we simply want to create a document-based application that is able to read and write files. The NSPersistentDocument class is a subclass of NSDocument that is designed to let you easily take advantage of the Core Data framework. By default, an NSPersistentDocument instance creates its own ready-to-use persistence stack, including a managed object context and a single persistent object store. There is in this case a one-to-one mapping between a document and an external data store.

The NSPersistentDocument class provides methods to access the document’s managed object context and provides implementations of the standard NSDocument methods to read and write files that use the Core Data framework. By default you do not have to write any additional code to handle object persistence. A persistent document’s undo functionality is integrated with the managed object context.

To Fetch Stored Data :

To retrieve data from a persistent store like a database SELECT operation. The result of a fetch is the creation of a collection of managed objects that are registered with the managed object context used to issue the request.

Fetch Request : An instance of NSFetchRequest that specifies an entity and optionally a set of constraints, represented by a NSPredicate object , and an array of sort descriptors (instances of NSSortDescriptor). These are similar to the table name, WHERE clause, and ORDER BY clauses of a database SELECT statement respectively. A fetch request is executed by being sent to a managed object context.


Fig:1.3_Demonstrating Fetch Request

To Insert Data :

The process of adding a managed object to a managed object context so that the object becomes part of the object graph and will be committed to a persistent store. Typically “insertion” refers only to the initial creation of a managed object. Thereafter, managed objects retrieved from a persistent store are considered as being fetched.

A managed object must be inserted into a managed object context before it is considered part of the object graph. A managed object context is responsible for observing changes to managed objects (for the purposes of undo support and maintaining the integrity of the object graph), and can only do so if new objects are inserted.

Features :

  • Core Data provides built-in management of undo and redo beyond basic text editing.

  • Core Data manages change propagation, including maintaining the consistency of relationships among objects.

  • It supports faulting which helps in reducing the memory over head of the program by lazily loading objects and also supports copy-on-write data sharing .

  • Core Data’s managed objects extend the standard key-value coding (KVC) validation methods that ensure that individual values lie within acceptable ranges so that combinations of values make sense.

  • In addition to synthesizing key-value coding (KVC) and key-value observing (KVO) compliant accessor methods for attributes, Core Data synthesizes the appropriate collection accessors for to-many relationships.

  • It also provides an option to integrate the application’s controller layer to support user interface synchronization, by providing the NSFetchedResultsController object on iOS, and integrates with Cocoa Bindings on OS X.

  • Automatic support for storing objects in external data repositories.

Conclusion :

Using core data reduces the amount of the code to be written to support the model layer of the application as it provides the various features to manage the data objects. It also eliminates the optimization of the code for managing data in the application as the framework has been highly optimized over several releases so far.

It takes advantage of information provided in the model and runtime features not typically employed in application-level code. Moreover, in addition to providing excellent security and error-handling, it offers best memory scalability of any competing solution.

Core Data is not a relational database or a relational database management system (RDBMS). It provides an infrastructure for change management and for saving objects to and retrieving them from storage thus behaving as an in memory store of the application. It uses the SQLITE as one of its persistent store .