In this tutorial, we discuss about the Core Data Relationship between entities. Persistent Storage has become an important part of the majority of iOS apps that are released today. When we tell about perseverance in iOS, we should only think on Core Data. This influential tool will offer a great experience for you while handling your data storage.
Core Data Relationships Example
For the principle of this tutorial, I have created a simple project with Core Data Entities that will handle both One-To-One and One-To-Manyrelationships.
There are 3 Entities created in the example:
Person: this will be the major entity that will have relationships with the Phone and Friends entities.
Phone: an entity that will remain the Person’s mobile phone information. It will be used as a One-To-One relationship, assume that the Person has only one phone.
Friends: an entity that will remain every Person’s friends. It will be used as a One-To-Many relationship, assume that the person has more than one friend.
As you can observe in the above image, I have already formed the relationships. I will now clarify to you how to that properly (it’s quite straightforward).
One-To-One Relationship (Person -> Phone)
If you have formed the Entities we can proceed with creating the relationshipbetween Person and Phone. You will want to include 3 values in order to build a relationship.
Relationship – name your relationship.
Destination – include the entity you want to start a relationship with (in our case Phone).
Inverse – craft an inverse relationship from Phone and select it under Person. Apple suggests that you always include an inverse value, so never leave this empty.
Every Entity includes its own automatically generated NSManagedObject that you can work within the code. This is one of the benefits of Core Data before others.
Here is an example how you can write in Person and its One-To-One Relationship (Phone).
let context = persistentContainer.viewContext let person = Person(context: context) person.firstName = "John" person.lastName = "Doe" let phone = Phone(context: context) phone.brand = "Apple" phone.model = "iPhone X" phone.os = "iOS" person.phone = phone saveContext()
One-To-Many Relationship (Person -> Friends)
I wish that by far you understood how relationships work. Now we will go advance and create a One-To-Many relationship. The concept is the similar as the One-To-One relationship, just with some minor adjust.
When creating a One-To-Many relationship, you will have to modify the type to To Many from the Data Model Inspector. This isn’t the case with One-To-One since this type is set to To One by default.
The following is an example how you can write in Person and its One-To-Many Relationship (Friends).
let context = persistentContainer.viewContext let person = Person(context: context) person.firstName = "John" person.lastName = "Doe" let friend1 = Friends(context: context) friend1.firstName = "Friend" friend1.lastName = "One" person.addToFriends(friend1) let friend2 = Friends(context: context) friend2.firstName = "Friend" friend2.lastName = "Two" person.addToFriends(friend2) saveContext()
The NSManagedObject includes generic methods like addToFriends() whereyou can pass either a Friends object or an array of Friends.