01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

236 CHAPTER 12 Reading and writing application data<br />

12.4.3 Defining the relationships<br />

As the Person and Task models stand right now, they won’t be of much use. You want<br />

to be able to add multiple tasks to a person. That means you must create a relationship<br />

between both of your entities: a task “belongs to” a person, and a person “has<br />

many” tasks.<br />

With the Data Modeling tool in the Table Editor style, select the Person entity and<br />

click the + button in the Relationships section. Call the relationship “tasks,” and select<br />

the Task entity as its destination. In the Data Model Inspector pane, next to Plural,<br />

check the To-Many Relationship box to tell Core Data that the Person entity can have<br />

more than one task. Finally, set the Delete Rule to Cascade. Cascade causes all the<br />

tasks that belong to a given Person entity to be deleted when the Person entity is<br />

deleted. Take, for example, a person called Peter. Peter has three tasks: go shopping,<br />

clean the car, and learn Core Data. When you delete Peter, those three tasks are<br />

deleted as well. The other two settings do the following:<br />

■<br />

■<br />

■<br />

Nullify—The child entities (in this case, the tasks) won’t be deleted, but they<br />

won’t “belong” to the deleted Person entity anymore. They’ll be orphans.<br />

Deny—Core Data won’t allow a parent entity to be deleted as long as it still has<br />

children. In this case, Peter can’t be deleted if he still has three tasks.<br />

No Action—Leave the destination objects as they are. That means they’ll most<br />

likely point to a parent entity that no longer exists. In this case, the three tasks<br />

would still belong to Peter even though he was deleted—a situation you generally<br />

want to avoid.<br />

You’ve specified that a person has many tasks; now you must specify that a task belongs<br />

to a person. Select the Task entity and add a new relationship called “person.”<br />

Uncheck Optional because you don’t want to be able to create unassigned tasks. The<br />

destination entity is obviously Person. The Inverse Relationship field lets you select<br />

the corresponding relationship in the destination entity (in this case, the tasks relationship<br />

of the Person entity). Why is that important? For data integrity, you always<br />

want to connect both sides of a relationship: you must always be able to access a person<br />

object’s tasks, and you must always be able to access a task object’s person. That<br />

way, you can change a task’s assignment from Peter to Mary, and Core Data knows it<br />

must update the task’s person to Mary and update Peter’s and Mary’s lists of tasks.<br />

With inverse relationships, you can always be sure your data stays consistent and Core<br />

Data does The Right Thing. Never forget to set up the inverse relationship.<br />

Leave the Delete Rule at Nullify because you don’t want a person to be deleted just<br />

because one of their tasks is deleted, but don’t want a finished task to stay connected<br />

to the person before it’s deleted.<br />

Your tasks relationship should look like figure 12.2, and your person relationship<br />

should look like figure 12.3.<br />

You’re done with the data model. Make sure to save it before moving on to the<br />

next step.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!