Relationships

One of the benefits of using Core Data is that entities can be related to one another in a way that allows you to describe complex models. Relationships between entities are represented by references between objects. There are two kinds of relationships: to-one and to-many.

When an entity has a to-one relationship, each instance of that entity has a reference to a single instance of another entity.

When an entity has a to-many relationship, each instance of that entity has a reference to a Set. This set contains the instances of the entity that it has a relationship with. To see this in action, you are going to add a new entity to the model file.

Open the Photorama application. In Photorama.xcdatamodeld, add an entity called Tag. Give it an attribute called name of type String. Tag will allow users to tag photos.

In Chapter 22, you generated an NSManagedObject subclass for the Photo entity. For Tag, you will let Xcode autogenerate a subclass for you through a feature called code generation. If you do not need any custom behavior for your Core Data entity, letting Xcode generate your subclass is quite helpful.

The NSManagedObject subclass for the Tag entity is already being generated for you. To see the setting that determines this, select the Tag entity and open its data model inspector. In the Class section, notice the setting for Codegen: It is currently set to Class Definition. This setting means that an entire class definition will be generated for you.

In Chapter 22, you used the Manual/None setting (which tells Xcode not to generate any code for the entity) for the Photo entity. The other code generation setting is Category/Extension, which allows you to define an NSManagedObject subclass with custom behavior while still allowing Xcode to generate the extension that defines the attributes and relationships.

A photo might have multiple tags that describe it, and a tag might be associated with multiple photos. For example, a picture of an iPhone might be tagged Electronics and Apple, and a picture of a Betamax player might be tagged Electronics and Rare. So the Tag entity will have a to-many relationship to the Photo entity because many instances of Photo can have the same Tag. And the Photo entity will have a to-many relationship to the Tag entity because a photo can be associated with many Tags.

As Figure 23.2 shows, a Photo will have a reference to a set of Tags, and a Tag will have a reference to a set of Photos.

Figure 23.2  Entities in Photorama

Entities in Photorama

When these relationships are set up, you will be able to ask a Photo object for the set of Tag objects that it is associated with and ask a Tag object for the set of Photo objects that it is associated with.

To add these two relationships to the model file, first select the Tag entity and click the Entities in Photorama button in the Relationships section. For the Relationship, enter photos. In the Destination column, select Photo. In the data model inspector, change the Type menu from To One to To Many (Figure 23.3).

Figure 23.3  Creating the photos relationship

Creating the photos relationship

Next, select the Photo entity. Add a relationship named tags and pick Tag as its destination. In the data model inspector, change the Type menu to To Many and uncheck its Optional checkbox.

Now that you have two unidirectional relationships, you can make them into an inverse relationship. An inverse relationship is a bidirectional relationship between two entities. With an inverse relationship set up between Photo and Tag, Core Data can ensure that your object graph remains in a consistent state when any changes are made.

To create the inverse relationship, click the pop-up button next to Inverse in the data model inspector and change it from No Inverse Relationship to photos (Figure 23.4). (You can also make this change in the Relationships section in the editor area by clicking No Inverse in the Inverse column and selecting photos.)

Figure 23.4  Creating the tags relationship

Creating the tags relationship

If you return to the Tag entity, you will see that the photos relationship now shows tags as its inverse.

Now that the model has changed for the Photo entity, you will need to regenerate the Photo+CoreDataProperties.swift file.

From the project navigator, select and delete the Photo+CoreDataProperties.swift file. Make sure to select Move to Trash when prompted. Open Photorama.xcdatamodeld and select the Photo entity. From the Editor menu, select Create NSManagedObject Subclass….

On the next screens, check the Photorama checkbox and click Next, then check the Photo entity checkbox and click Next. Make sure you are creating the file in the same directory as the Photo+CoreDataClass.swift file; this will ensure that Xcode will only create the necessary Photo+CoreDataProperties.swift file. Once you have confirmed this, click Create.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset