Monday, August 14, 2017

Swift 3 - CoreData - To One - Inverse Relationship (Tutorial-4)

In the last tutorial we have seen 'To One - No Inverse relationship' with a person address as example.

In this tutorial, Let’ see To One - Inverse relationship.

Please checkout code for this example here

In this example user can add spouse which is also of Person class and each person can access spouse with, spouse property.





Here, 

We are having a Inverse relationship, Because person’s spouse can also access spouse which is an Inverse Relationship.

The Type of Relationship here is To One here as each person can only have one spouse here.

The Delete Rule for this relationship is Nullify as we don’t want spouse should get deleted If a person gets deleted.
Spouse value becomes nil, If a person’s spouse object will get deleted. That is why it is called Nullify

Relationship - spouse
Destination - Person (Could be a different type also)
Inverse - spouse



Download the example project and add person and spouse, Delete spouse and see the result.

The beauty of Inverse relationship is, When we are adding a spouse to a person, We don't need to explicitly tell like the person's spouse's spouse is the person again. The Inverse relationship is taken care by the CoreData. 

See the screenshots below for better understanding of Inverse Relationship.


Add Person
Add Spouse




Person's Spouse

Person's spouse's spouse


Hope this post is useful. Feel free to comment incase of any queries.


No comments:

Post a Comment