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

To assign or change a field value in the record, use setValue:forKey:. Pass the value as a Cocoa object and the field label as an NSString. To read a field value, use valueForKey:. Again, pass the field label as an NSString.

Each record field is represented by an NSAttributeDescription or an NSRelationshipDescription class (Figure 8). But we usually do not interact with these two classes. Nor do we create them explicitly. Instead, we use an Xcode feature to define the record schema.

Figure 8: The two classes for representing a record field.

Introducing the Schema Editor

Starting with Xcode 4.x, only three iOS project templates have optional support for Core Data (Figure 9). The other templates do not, but you can add to them the files and frameworks needed for Core Data.

Figure 9: The templates that have built-in support for Core Data.

The Master-Detail template creates the traditional navigation-style app. It provides two panel views, one to show a list of records, another to show a record's details. The Utility template creates an app with a single panel view. It is meant for loading and viewing records one at a time. Finally, the Empty template is generic. It does not provide any panel views, giving you the freedom to define your own.

Suppose you want to use the Master-Detail template. Select the template from the Project Assistant and click Next. On the options panel (Figure 10), fill in the project details. Make sure to set the checkbox labelled Use Core Data. Click Next and set the project's location and source repository. Then click Create to save the project and its support files.

Figure 10: The options panel of the Project Assistant.

Figure 11 lists the files supplied by the template. One file, FooTriage.xcdatamodeld, will carry the database schema being defined. Selecting it displays the new schema editor shown on the right. The editor then divides the schema into three main groups. For now, we will focus only on the Entities group, which the editor shows by default.

Figure 11: The files supplied by the template.

Next, the schema editor divides each Entities group item into three subpanels. The Attributes subpanel will house the record fields that hold user data.Tthe Relationships subpanel will house fields that link one table entity to another. And the Fetched Properties subpanel will house fields that hold weak static queries.

Below the schema editor is its toolbar. The Add Entity button inserts a new table entity to the database schema. The Add Attributes button inserts a new field attribute. Both buttons use a popup-menu to list alternate options. And the Editor Style button changes the editor's display mode.

Defining the Database Schema

Let's use the schema editor to define the database structure in Figure 12. This is a bare-bones triage database used by some medical applications. The Person table is the primary table. It holds specifics about a patient such as name, age and gender. The Vitals table is the secondary; it holds the health readings for each patient.

Figure 12: The database structure of the sample app.

The Injury table assigns each patient one of eight possible injuries. The Status table files each patient under one of four possible states. Both Injury and Status are constant tables; their records stay unchanged during usage.

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.