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

Handheld devices such as the iPhone and iPod/Touch are portable repositories for a user's personal data. The data may be documents, but it often takes the form of databases. For an iOS application to use a database, it relies on a Cocoa API known as "Core Data." In this article, I examine how Core Data works within an iOS app. I start by explaining how Core Data defines a database structure and what class represents each part of the structure. Then, I detail how Core Data handles a typical file cycle, and how it handles a record transaction. You will need a working knowledge of Objective-C and Xcode a basic understanding of database design.

Core Data and the Database

Core Data gives the iOS app a standard, near-transparent interface to a database file. With Core Data, an app can define a database schema, create a database file, and create and manage record data. Core Data is hardware-agnostic. Runtime support is available on the same processors that MacOS X and iOS support. Plus, Core Data works directly with SQLite, the public-domain database engine bundled with MacOS X and iOS.

Figure 1 shows how a typical database appears under Core Data. The database file is seen as a persistent store. It retains record data between separate app sessions, even after iOS itself shuts down. In memory, the database appears as a managed object context. The context holds a copy of the database schema, and maintains both new and changed records in a queue. It also handles all queries made against the database. The context stays resident only for the lifetime of the app.

Figure 1: How Core Data views a database.

The data flow between context and store is mediated by a coordinator object. Each database table is represented as an entity, each record a managed object. There can be several managed objects within the same entity, and several entities within the same context. A record field is either an attribute or a relation. In the latter type, the field holds a reference to a secondary record.

Core Data and its Classes

At least 20 Cocoa classes make up the whole Core Data framework. Out of those, nine classes play major roles in all database sessions, and it’s important to learn what they do. The following class diagrams show just a subset of methods for each class. Consult the official Apple docs for a complete list.

The NSPersistentStore class (Figure 2) interfaces directly with the database file. It supports three basic file types: SQLite, XML, or a custom binary. It can handle files stored on a local volume or on a network server.

Figure 2: The NSPersistentStore class.

You usually do not create an explicit instance of NSPersistentStore. Instead, use NSPersistentStoreCoordinator (Figure 3) for that task. That class creates the coordinator that links each persistent store to the object context. It serializes the record data, rendering the data into a form suitable for storage. It can move the data from one database file to another. It can work with multiple database files, and it creates a persistent store for each file.

Figure 3: NSPersistentStoreCoordinator.

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.