Core Data: Entity Relationships

Ravi Kumar May 22, 2019

Core Data: Entity Relationships

Core Data Entity Relationships

In my previous article Core Data: CRUD Operations, we learned about Core Data CRUD operations i.e. how to add, edit, delete and read data from entities.

In this article, we will learn about the relationship between entities i.e. how the records of ‘one entity’ are related to the records of ‘another entity’. We will use the ‘User’ and the ‘Post’ entities created in the previous article to understand the concept of relationships.

Overview

CoreData entity relationships help us to associate records of one entity with the records of another entity. Let us understand it with the help of an example. In our Blogger project, we have CoreData entities namely User and Post. A single user can have any number of blog posts and to relate the blog posts to its blogger (user), we use relationships. In other words, we can say, “We need a relationship between the ‘User’ and ‘Post’ entities to access the posts related to a user”.

Let’s have a look at some frequently used terms:

Relationship Name
It is the name of a relationship.

Source Entity
It is the entity from which we have to create a relationship with another entity.

Destination Entity
It is the entity whose one or more records are related to a particular record of a ‘Source Entity’.

Inverse Relationship
Apple requires us to provide an inverse relationship to each relationship. For example, if a ‘User entity’ has a relationship post to the ‘Post entity’, then we need to provide an inverse relationship to this post’s relationship from the ‘Post entity’ to the ‘User entity’.

To-Many Relationship
If one record of a ‘Source Entity’ has multiple related records in the ‘Destination Entity’ then the relationship is called ‘To-Many’ relationship. For example, a single user can have multiple posts.

To-One Relationship
If one record of a ‘Source Entity’ has only one related record in the ‘Destination Entity’, such kind of relationship is called ‘To-One’ relationship. For example, each post is related to only a single user.

Also Read- Core Data: Core Data Stack in Swift

posts relationship from User to Post

The following image displays a relationship named ‘posts’ from the User Entity (Source) to Post Entity (Destination). As we have discussed earlier, each relationship needs to have an inverse relationship, this relationship from User to Post has an inverse relationship named ‘user’.

This relationship is a ‘To-Many’ relationship as the user can have multiple posts.

user relationship from Post to User

The following image displays a relationship named ‘user’ from Post Entity (Source) to the User Entity (Destination). As we have discussed earlier, each relationship needs to have an inverse relationship, this relationship has an ‘Inverse’ relationship named ‘posts’ (Post to User).

This relationship is a ‘To-One’ relationship as a post can be related to only one user.

Delete Rule

Each relationship has a delete rule. Delete rule defines ‘what happens to related records when a record owning the relationship is deleted’. For example, ‘What happens to all the posts of a user if the user record is deleted’.

There are four delete rules as displayed in the image below:

No Action

If the delete rule ‘No Action’ is applied and the record owning the relationship is deleted, nothing will happen to the related records.

For example, if a user is deleted then nothing will happen to the posts posted by that user. Related posts will continue to believe that they are still related to a user but I think this rule is not logical and I never use it in my applications.

Nullify

This delete rule nullifies all the relations between the ‘relationship owning record that is to be deleted’ and the ‘related records’. For example, if a post is deleted and delete rule is Nullify, the post will be deleted. Also, the user related to that post will get to know that he does not have that post anymore.

Cascade

This delete rule will delete all the related records if the relationship owning record is deleted. For example, if a user is deleted then all his posts will also be deleted.

Deny

This delete rule will not let you delete the relationship owning records if there are any related records. You need to delete all the related records if you want to delete the relationship owning record.

In this article, we have learned about CoreData entity relationships.

Feel free to contact me for any further clarification on the subject matter.

Stay Tuned: In my next article, we are going to discuss CoreData Lightweight Migration.

If you enjoyed this blog post, share it with your friends!

Cheers!!!

Also Read- Core Data: Core Data Stack in Swift

Lets’s Talk

About your ideas and concept