Database

Understanding Core Data on iOS

By José R.C. Cruz, July 31, 2012

Using a database in iOS requires careful navigation of the classes in Core Data. Here's a detailed look at how to use Core Data to create and use a simple database.

To define the Person table, first click the Add Entity button on the toolbar. Double-click on the entity placeholder and change its name to"'Person." Next, click Add Attributes and rename the inserted placeholder to "uid." Make sure to select Person from the Entities group and to expand the Attributes subpanel. Click under the Type column and choose Integer 32 as the field's datatype. Repeat these two steps for the other three record fields. When done, the Person table should look like the one in Figure 13.

Figure 13: The Person table, as defined.

Now, use the same procedure to define the secondary Vitals table. To relate this table with Person, select Person from the Entities group, then click and hold on the Add Attributes button. From the ensuing pop-ip menu, choose the menu item Add Relationship. Make sure again to expand the Relationship subpanel before proceeding.

Rename the relationship placeholder to "vitals" (all lowercase). From the Destination column, choose "Vital," and from the Inverse column, choose "No Inverse." This creates a simplex relation between the Person and Vitals table. Save your changes before proceeding. Repeat this process when defining the constant table Injury (Figure 14).

Figure 14: Defining the constant table, Injury.

On the Person table, add a relationship named "injury." Set its destination to "Injury," but choose "Person" as the inverse. Then on the Injury table, add a relationship named "uid.". Set its destination as the Person table, its inverse Injury. The result is a duplex relation between the Person and Injury tables.

Do the same for the constant table Status. Make sure to use "status" as the field relating this table from the Person table. Save your changes and click the Editor Style button to change display modes. The editor will show all four tables, their fields and relations, in graphical form (Figure 15). The duplex relations appear as double-headed arrows, the simplex ones as single-headed arrows.

Figure 15: A graphical representation of the four tables in the application.

Note that I have not used a single CREATE TABLE command to define any of the tables.

Working with the Database File

The database file cycle has three major parts (Figure 16). In the first part, the iOS app creates and links the Core Data objects it needs. In the second, it connects the database file to the managed object context. And in the third and last part, it writes any pending changes from the database to the file just before it terminates.

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.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!