23
Core Data Relationships

Core Data is not that exciting with just one entity. Much of the power behind Core Data comes to light when there are multiple entities that are related to one another, because Core Data manages relationships between entities.

In this chapter, you are going to add tags to the photos in Photorama with labels such as Nature, Electronics, or Selfies. Users will be able to add one or more tags to photos and also create their own custom tags (Figure 23.1).

Figure 23.1  Final Photorama application

Final Photorama application

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 will have a reference to an instance in the entity it has a relationship to.

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.

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

Unlike with the Photo entity in Chapter 22, you will not generate an NSManagedObject subclass for the Tag entity. Instead, 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 for you 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. The other code generation settings are 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) and Manual/None (which tells Xcode not to generate any code for the entity).

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. 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 + button in the Relationships section. Click in the Relationship column and enter photos. In the Destination column, select Photo. In the data model inspector, change the Type dropdown from To One to To Many (Figure 23.3).

Figure 23.3  Creating the photos relationship

Screenshot shows the creation of 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 dropdown 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 dropdown 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.)

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

Figure 23.4  Creating the tags relationship

Screenshot shows the creation of tags relationship.

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 screen, check the box for Photorama and click Next. Check the box for the Photo entity 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