A relationship in Core Data adds a related object as a property to another object. In our case, we want to define a relationship between FamilyMember and Movie. The relationship we're looking for is a one-to-many relationship. This means that every movie will have only one family member associated with it and every family member can have multiple favorite movies.
This setup is not the most efficient one since a many-to-many relationship would allow us to reuse a single movie for multiple family members. By using a one-to-many relationship, we're forcing ourselves to create a duplicate movie instance if multiple family members want to add the same movie. For the sake of keeping our setup simple, this is okay. However, if you're building your own application, it is worth considering a many-to-many relationship since it's more flexible and less wasteful.
Select the FamilyMember entity and click on the plus icon at the bottom of the Relationships list. Name the relationship favoriteMovies and select Movie as the destination. Don't select an Inverse relationship yet because we haven't defined it yet. The Inverse relationship will tell the model that Movie has a property that points back to the FamilyMember. In the Data Model inspector, make sure you select To Many as the type for this relationship. Also, select Cascade as the value for the delete rule.
The delete rule is a very important property to be set correctly. Not paying attention to this property could result in a lot of orphaned and even corrupted data in your database. For instance, setting this property to nullify simply sets the Inverse of the relationship to nil. This is the behavior we'd like to see when we delete a movie. Deleting a movie shouldn't delete the entire family members who added this movie as their favorite. It should simply be removed from the list of favorites.
However, if we delete a FamilyMember and nullify the relationship, we end up with a bunch of movies that don't have a family member associated with them. In our application, these movies are worthless; we won't use them anymore because every movie only belongs to a single FamilyMember. For our app, it's desirable that, if we delete a FamilyMember, we also delete their favorite movies. This is exactly what the cascade option does; it cascades the deletion over to the relationship's Inverse.
After setting the delete rule to cascade, select the Movie entity and define a relationship called FamilyMember. The destination should be FamilyMember, and the Inverse for this relationship is favoriteMovies. After adding this relationship, the Inverse will be automatically set on our FamilyMember entity:
![](assets/97b87bcf-98fb-4b80-a6f0-c9a4d4b4aae2.png)
With the data models in place, we can start creating our NSManagedObject subclasses.