Core Data Story: an introduction to iPhone SDK database abstraction technology

iphone-sdk-core-dataSo, what’s Core Data anyway? :)
Core Data helps you to save model objects (in the sense of the model-view-controller design pattern) to a file and get them back again. This is similar to archiving technology but Core Data offers much more than that. Amongst other things it…

  • Provides an infrastructure for managing all the changes to your model objects. This gives you automatic support for undo and redo, and for maintaining reciprocal relationships between objects.
  • Allows you to keep just a subset of your model objects in memory at any given time. This is especially important on iPhone where conserving memory is critical.
  • Uses a schema to describe the model objects. You define the principal features of your model classes—including the relationships between them—in a GUI-based editor. This provides a wealth of basic functionality “for free,” including setting of default values and attribute value validation.
  • Allows you to maintain disjoint sets of edits of your objects. This is useful if you want to, for example, allow the user to make edits in one view that may be discarded without affecting data displayed in another view.
  • Has an infrastructure for data store versioning and migration. This lets you easily upgrade an old version of the user’s file to the current version.

Important : Core Data is available on iPhone OS v3.0 and later. This document describes tools and techniques for iPhone OS v3.0.

But how does it work? ;)
Core Data application starts with building three objects (that are instances of classes that are listed below) which are main parts of application :

  1. NSManagedObjectModel
  2. NSManagedObjectContext
  3. NSPersistentStoreCoordinator

NSManagedObjectContext – is a bridge between database’s data and user interface that enables real model-view-controller structure. This object is in charge for managing all data (all object’s) from database and that means stuff like capture data (sorting, predicate…), deleting data, updating data, inserting new data… Its primary responsibility is to manage a collection of managed objects. The context is a powerful object with a central role in your application, with responsibilities from life-cycle management to validation and relationship maintenance.

NSManagedObject – conceptually, NSManagedObject it’s an object representation of a record in a table in a database, so it’s a model (in the sense of the model-view-controller design pattern) object that is managed by Core Data. A managed object is always associated with a managed object context. When you create a new managed object, you insert it into a context. You fetch existing records in the database into the context as managed objects. Any changes you make (whether insertion or deletion of complete objects, or manipulation of property values) are kept in memory until you actually commit them to the store by saving the context.

One more controller you can use is NSFetchedResultsController. This controller enables application to accept some protocols. It is mostly used by applications that can edit database data. Example of this kind of application is To-do application where you have list of to-do’s.

Lets have small user story here. Meet user Mauro. Let’s imagine that he enters To-do application to add new to-do (for example – “Remember Ivan to write a new blog post on Surgeworks blog”) note to existing list of to-do’s. Looking from developer perspective application just created new subclass of NSManagedObject with note’s data in it. To save this new object we just have to add it to our instance of NSManagedObjectContext and call “save” method. Now NSFetchedResultsController delegate class knows there is update in database (one more to-do) and it automatically notify’s delegate class. Use of change notifications can be very usable especially when building larger application’s.

This top level approach simplifies and enables work with database data without one row of SQLite statement. One of the most important fact is that you don’t have to use primary key’s because core data framework creates his own. In fact, Apple’s documentation says not use primary keys. Now you can actually pass live objects to NSManagedObjectContext and it to database with one row of code.

So, what’s .xdatamodel file for? :))

When you run core data driven application for first time application will create SQLite database based on .xdatamodel in your project. In more detail, application will build SQLite database structure from NSManagedObjectModel instance that is loaded from .xdatamodel from [NSBundle mainBundle].

In xdatamodel you can make entity’s and connections between them on a very simple way through graphical interface. In fact, you can add NSManagedObject subclass in your project with all property’s add make reference on them from xdatamodel or you can declare them just in xdatamodel file.

NSManagedObjectModel – is in charge for loading application’s .xdatamodel ( comparison – something like loading view from nib).

That’s all from me for now! Cheers!