In my first article “Data Storage in iOS”, I talked about the different types of data storages available to the iOS developer. With this post I would show you how you could start off with Core Data.

Create a new  Empty Application template project in Xcode 4.2 and choose Use Core Data and name the project CoreDataExample.

Now let’s take a quick look at the project. First expand CoreDataExample folder and click on CoreDataExample.xcdatamodeld. You’ll see a visual editor on right side. This is what we’ll be using for designing model objects.

Take a look at AppDelegate.m. You will see new functions related to core data which are implemented for us are managed object context, managed object model and  persistent store coordinator. You might have gone into a shock seeing this, but no worries !! These are pretty simple stuff.

Managed Object Model: It is a class that contains definitions for each of the Entities that you are storing in the database.

Persistent Store Coordinator: It is like database connection. Here’s where we specify location of the database which will be used to store the Entities.

Managed Object Context: Using this we can do CRUD operations like get objects,insert objects, or delete objects.

Creating the Entity
Now you need to open the visual model editor (Click on CoreDataExample.xcdatamodeld).
To create entity click on the Add Entity button in the bottom of the Editor and enter the name of the entity , in our example give it as contacts, like the following:

So, lets add some attributes .First select the entity and click on the add attribute button on bottom right or click on the plus button in the attribute box in the editor. In our example, add name by specifying the type as String, as shown:

Creating our model files
We need to create a NSManagedObject class of the entity .It is a simple process.Select the CoreDataExample.xcdatamodeld and go to Editor>Create NSManagedObject subclass option in the menu .A pop-up will appear and click on create button ,as shown below:

So now we have two new files in our application. Contacts.m and Contacts.h. They define the NSManagedObject class for the Contacts entity .

Adding view controller
We have chosen Empty Application template to add Core Data in our application. We need to create our own UIViewController to wire up core data with user interface so, name the new UIViewController as NameTableViewController.
After adding view controller we need to modify AppDelegate.m file to make the new view controller as root view controller. Do the following changes in AppDelegate .h and .m file:

Saving Data
Create a NSMutableArray nameArray for storing data fetched from Core Data. Add UIBarButtonItem rightBarButtonItem in the navigation bar and create a selector for it for showing UIAlertView with UITextField to save name  in the NameTableViewController, like the following:

Add the following UIAlertView didDismissWithButtonIndex delegate method for handling the save contact operations:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context  = [appDelegate managedObjectContext];

Contacts *contact = (Contacts *)[NSEntityDescription insertNewObjectForEntityForName:@”Contacts” inManagedObjectContext:context];

[contact setName:nameField.text];

nameField.text = @””;

NSError *error;
if(![context save:&error]){
//Handling error
[self.nameArray insertObject:contact atIndex:0];
[self.tableView reloadData];


The above code creates App delegate instance and uses it to call managed object context. This context is then used to create a new managed object Contacts entity. The setName method is used set the name attribute of the managed object and finally the context is instructed to save by calling the context’s save method.

Retrieving the data


AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@”Contacts” inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSError *error;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];

if (!mutableFetchResults) {


[self setNameArray:mutableFetchResults];


Here we create a new object called a fetch request. It is like the SELECT clause. We call entityForName to get a pointer to the Contacts entity we want to retrieve, and then use setEntity to tell our fetch request that’s the kind of Entity we want.
Which is equivalent to “select * from Contacts”
We then call executeFetchRequest on the managed object context and pull all of the objects of the Contacts in the nameArray.

With the fetched data stored in the nameArray we can now populate UITableview.

Now build and run the application.This how it looks.