Mastering In CoreData (Part 5 Relationship Between Entities in Core Data)
For the purpose of this part we will extend our Todo application. Final app will handle both One-To-One and One-To-Many relationship. We will be covering Many-Many in later part.
There are three Entities in the example project
1. User → This will be the main entity, that will have relationships with the Task and Passport entities
2. Task → This will contains the Todo task of the user, will have Many-To-One relationship with User
3. Passport → This will contain the passport information of a user, will have One-To-One relationship with User
These are all the Entities created so far as shown in Figure 1,2 and 3. If you don’t know how to add property refer to part 3
There are a number of things you have to decide when you create a relationship and these options are available on right side in the Data Model inspector tab as shown in Figure 4
- Destination entity → Destination Entity name you want to create relationship with
- Cardinality → whether a relationship is One-To-One, Many-To-One
- Optional → relationship can be NULL / NOT NULL
- Transient → In memory usage
- Limit → If it’s a to-many, are there maximum or minimum numbers of objects that can be in the relationship? (The lower limit does not have to be zero)
- Direction → Most object relationships are inherently bidirectional. If a department has a To — Many relationships to the Employees who work in a Department, there is an inverse relationship from an Employee to the Department that is To-One . The recommended approach is to model relationships in both directions and specify the inverse relationship appropriately. Core Data uses this information to ensure the consistency of the object graph if a change is made
- Delete Rule → A relationship’s delete rule specifies what should happen on the destination Entity if an attempt is made to delete the source object.
Deny → If there is at least one object at the relationship destination (employees), do not delete the source object (department).
Nullify → Remove the relationship between the objects, but do not delete either object.
Cascade → Delete the objects at the destination of the relationship when you delete the source.
No Action → Do nothing to the object at the destination of the relationship.
No Action rule might be of use, because if you use it, it is possible to leave the object graph in an inconsistent state (employees having a relationship to a deleted department).
One-To-One Relationship (User →Passport)
Now we create a relationship of a User with a Passport. User can have one passport associated with it.
Go to the User Entity → Tap on + on Relationships tap → Add relation details as shown in figure 5
For the inverse relationship we need to create relationship on other side as well
Go to the Passport Entity → Tap on + on Relationships tap → Add relation details as shown in figure 6
Now we need to decide delete rule. What if User is deleted there is no need to persist user passport so we make User → Passport delete rule should be cascade as shown in Figure 7, which means when User 1 is deleted, core data automatically delete all passports associated with the User 1.
Secondly we choose To One on Type tap of Data Model inspector, since the relationship between User →Passport is To-One
On the other hand if we delete Passport, user still remain in the database so we make Passport → User delete rule to Nullify . Also since relationship is One-To-One we choose To One on Type tab as shown in Figure 8
One-To-Many Relationship (User →Task)
Now we create a relationship of a User with Tasks. User can have multiple todo tasks associated with it.
Go to the User Entity → Tap on + on Relationships tap → Add relation details as shown in Figure 9
For the inverse relationship we need to create relationship on other side as well
Go to the Task Entity →Tap on + on Relationships tap → Add relation details as shown in Figure 10
Now go to the User Entity and Task Entity and select inverse relationship as shown in Figure 11 and 12
Now we need to decide delete rule. What if User is deleted there is no need to persist user tasks. So we make User →Task delete rule cascade as shown in Figure 13. For cardinality, User can have multiple To-do Tasks.
Go To User Entity → tap on User → Task relationship → On the right hand side window select To-Many on Type tab in Data Model inspector as shown in Figure 13
On the other hand if we delete Tasks, user still remain in the database so we make Task → User delete rule to Nullify as shown in Figure 14. For cardinality, task can only have one User associated to it for the sake of simplicity.
Go To Task Entity → tap on Task → User relationship →On the right hand side window select To-One on Type tab in Data Model inspector as shown in Figure 14
Since we added new attributes to an existing entity and created some new Entities as well, we need to again generate classes for the existing Entities using Xcode’s built-in functionality to generate the classes, in the same way as you did in part 4. You need to delete existing Entities for that by doing Right click On Classes → Delete → Select Move to Thrash
Finally, generated NSManagedObject classes for all Entities as shown in Figure 15
Since User can have multiple tasks associated to it. We can see in User class their is a NSSet property which is representing their tasks array associated to the User. Also Xcode has generated some basic CRUD operations as shown in Figure 15.
Summary
We just finished how we can create relationship between Entities. We also looked configuration required to create relationship.
What Next?
In the next part we will be doing CRUD operation in these entities having relationship in object oriented style