Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [84]

By Root 950 0
and a “to-one” relationship rooted at the second entity and ending at the first. You then tell specify in your model that these two are inverse relationships, and Core Data then understands the bidirectional nature of what you’ve configured. This setup allows you, if you wish, to create a relationship that is truly unidirectional, though in practice it’s questionable whether that’s ever really desirable. The examples in this book all use bidirectional relationships.

So, let’s create these relationships, starting with a one-to-many relationship from MythicalBand to MythicalPerson. Select MythicalBand, and click the “+” button below the Property table, just as you would for creating an attribute, but this time select Relationship from the popup menu. This creates a new relationship, whose configuration options (see Figure 8-5) look a little different than what you’ve seen when creating attributes.

Figure 8-5. Configuration options for a new relationship

You’ll see a Name field, and checkboxes for Optional and Transient, but otherwise it’s all different. The Destination popup lets you choose where the other end of the relationship goes, and after setting that the Inverse popup lets you choose which other relationship (if any) should be used to create a bidirectional relationship. A checkbox lets you specify that this is a to-many relationship, and if that’s checked, two fields are enabled that let you put handy constraints and the minimum and maximum number of objects may be attached on the other end. Finally, the Delete Rule popup lets you decide what should happen to objects at the other end of the relationship if the “source” of the relationship is deleted. The most common choices are Nullify (which means that the “source” is removed from any inverse relationship in the “destination” element), Cascade (which means that the “destination” object or objects are also deleted), and Deny (which means that if this relationship is present, the “source” object will refuse to be deleted at all). A fourth alternative, No Action, leaves the inverse relationship intact, leaving the developer with the task of taking care of it (or risk ending up with data integrity problems). We won’t use that option at all.

Name the relationship you’ve just created “members,” and set its destination entity to MythicalPerson. Click to turn on the To-Many Relationship checkbox, and leave the Delete Rule set to Nullify (which is the default value). Congratulations, you’ve just made your first unidirectional relationship! Doing this simple step gives the MythicalBand entity a new members property with a whole set of new features. Each band can now provide an array of MythicalPerson objects, and that array will be automatically maintained in line with the options you configured. In the graph paper area, you’ll see a unidirectional arrow, with a sort of double head, from MythicalBand to MythicalPerson.

Now let’s create the inverse of this relationship. Select the MythicalPerson entity, and add a new relationship. Set its destination entity to MythicalBand, and then set its inverse to members. The rest of the default values can be left as they are. You’ll now see a bidirectional arrow between MythicalPerson and MythicalBand, with a single head at the band end.

Let’s move on to the one-to-many relationship between MythicalBand and MythicalGig. Select MythicalBand, create a new relationship named “gigs,” with MythicalGig as its destination, and the To-Many Relationship checked. Because a gig without a band doesn’t do us any good, set the Delete Rule to Cascade. That way, if a band is deleted, all its gigs will be deleted, too. Now select MythicalGig and make a new relationship called “band,” with MythicalBand as its destination, and select gigs as its inverse relationship. Here, we can leave the Delete Rule set to its default value, Nullify. This simply means that if a gig is deleted, any trace of the gig on remaining end of the relationship to the band will be wiped out, but the band itself will be left intact.

Finally, it’s time for the one-to-many

Return Main Page Previous Page Next Page

®Online Book Reader