Channels ▼
RSS

Database

Understanding Core Data on iOS


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.


Figure 16: The iOS database app process.


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.
 

Video