Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Embedded Systems

Understanding Core Data on iOS

Listing One shows how we might implement the database file cycle in a custom class. The class, FooTriage, declares three protected properties (lines 4-6) for three Core Data classes. In its init method (line 22), FooTriage invokes prepModel, which creates an instance of NSManagedObjectModel using initWithContentsOfURL: (lines 61-62). To that init method, it sends the path to the resource file FooTriage.momd. This file sits inside the application bundle (lines 54-56), and it contains the database schema defined earlier. The prepModel method ends by storing the NSManagedObjectModel instance into the property dbModel.

// -- Declaring the class interface
@interface FooTriage : NSObject 
	NSManagedObjectContext *dbContext;
	NSManagedObjectModel *dbModel;
	NSPersistentStoreCoordinator *dbSource;
- (void)prepModel;
- (void)link2file;
- (void)save2file;

// -- Implementing the class
@implementation FooTriage

// Initialising the class
- (id)init
	// initialise the parent
	if (self = [super init])
		[self prepModel];
		[self link2file];
		return (self);
		// the parent failed to initialise
		return (nil);

// Disposing the class
- (void)dealloc
	// save any database changes
	[self save2file];
    // dispose the following properties
	[dbContext release];
    [dbModel release];
    [dbSource release];
	// invoke the parent method
    [super dealloc];

// Preparing the object model
- (void)prepModel
	NSString *tPth;
	// set the schema location
	tPth = [[NSBundle mainBundle] 
	tURL = [NSURL fileURLWithPath:tPth];
	// load the schema
	dbModel = [[NSManagedObjectModel alloc] 

// Linking to the database file
- (void)link2file
	NSError *tErr = nil;
	BOOL tChk;
	// set the file location
	tURL = [NSURL fileURLWithPath:[[self 
	// create the coordinator
	dbSource = [[NSPersistentStoreCoordinator alloc]
	if (dbSource != nil)
		// initialise the coordinator
		tChk = [dbSource 
			configuration:nil URL:tURL 
			options:nil error:&tErr];
		if (tChk)
			// prepare the object context
			dbContext = [[NSManagedObjectContext alloc] 

// Saving changes into the file
- (void)save2file
    NSError *tErr = nil;
	BOOL tChk;
	// check for a valid context
	tChk = (dbContext != nil);
	if (tChk)
		// does it have pending changes?
		tChk = [dbContext hasChanges];
		if (tChk)
			// update the database file
			tChk = [dbContext save:&tErr];
	// check the save outcome
	if (tChk)
		NSLog(@"successful save");
		NSLog(@"failed save");
Listing One.

Next, FooTriage invokes the method link2file (line 23). This method makes an instance of NSPersistentStoreCoordinator and links it with the property dbModel (lines 78-79). It also stores the instance into the property dbSource. Then the link2file method preps dbSource with the method addPersistentStoreWithType… (lines 83-86). To that method it sends the path to the database file FooTriage.sqlite and an NSError object. When addPersistentStoreWithType… returns a YES, link2file creates an instance of NSManagedObjectContext (lines 90-91) and stores it in the property dbContext. It then connects dbContext to dbSource using the method setPersistentStoreCoordinator: (lines 92-93).

In its dealloc method (line 36), FooTriage invokes its method save2file. This method checks the property dbContext for a valid instance of NSManagedObjectContext and for any pending changes (lines 107-111). If both are true, save2file calls the save: method (line 114), passing along an NSError object for input. Afterwards, FooTriage disposes of each property with a release message and invokes its parent's dealloc method (lines 39-44).

Working with Database Records

A typical record transaction can be one of four basic tasks: retrieval, insertion, edition, and deletion. Except for retrieval, these tasks alter the database by adding new records or changing existing records. The alterations are always queued to allow for undos and to protect the database file from unwanted changes.

Listing Two shows how to prepare a constant table. This method, initInjury, adds eight records to the table Injury. It starts with a sequence of injury terms, each term delimited by a '%' token (line 11). It then uses the NSString method componentsSeparatedByString: to divide the terms and hold them in an NSArray object (line 13).

- (void)initInjury 
	NSString *tInj, *tNom;
	NSUInteger tIdx, tKey, tCnt;
	NSArray *tLst;
	NSError *tErr = nil;
	NSManagedObject *tRec;
	// define the list of injuries
	tInj = @"Blunt Trauma%Burn%C-Spine%Cardiac%Crushing%Fracture%Laceration%Penetration";
	tLst = [tInj componentsSeparatedByString:@"%"];
	tCnt = [tLst count];
	// start adding the categories
	for (tIdx = 0; tIdx < tCnt; tIdx++)
		// create a new record
		tRec = [NSEntityDescription 
		if (tRec != nil)
			// read an injury name
			tNom = [tLst objectAtIndex:tIdx];
			// prepare the record
			tKey = tIdx;
			[tRec setValue:[NSNumber numberWithInt:tKey]
			[tRec setValue:tNom forKey:@"name"];
			// insert the record
			[dbContext insertObject:tRec];
	// ...commit the records to make them available

Listing Two.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.