Tuesday, July 25, 2017

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

In my earlier CoreData tutorials, We have gone through CRUD operations and validations. 

In this tutorial, Let’s start with Relationships.

I am taking an example where, person will have a address and the relationship of a person with address is To One and it is No Inverse. That means, person knows about the address and address doesn’t know the person.
If we delete person object, address also will get deleted If we set the delete rule as Cascade.

Please check out the code for this example from here  








I have created an Address entity. Person entity holds the relationship as address which of Address(Destination) class. 
















There is No Inverse Relation here. That is why address doesn’t know about person. The delete rule here is Cascade. If person is deleted, address will also get deleted.
The type of relationship is To One. Each person will have only one address.

Go through the example link and try to add person with address. Check the sqlite file that core data creates. With our applied relationship and delete rule, If a person is deleted, address will get deleted from database automatically.

In the next tutorial, Let’s see a different relationship type :)

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



No comments:

Post a Comment