Core Data Relationshipsโ€‚by@dejanatanasov

Core Data Relationships

image
Dejan Atanasov HackerNoon profile picture

Dejan Atanasov

Senior iOS Developer

Understanding One-to-One and One-To-Many relationships

While working on my latest project I have decided to write a tutorial about Core Data Relationships between Entities.

Persistent Storage has become an essential part of the majority of iOS apps that are released today. When we speak about persistency in iOS, we should only think on Core Data. This powerful tool will provide a great experience for you while managing your data storage.

Core Data Relationships Example

For the purposes of this tutorial, I have created a simple project with Core Data Entities that will handle both One-To-One and One-To-Many relationships.

There are 3 Entities created in the example:

  1. Person - this will be the main entity, that will have relationships with the Phone and Friends entities.
  2. Phone - an entity that will keep the Personโ€™s mobile phone information. It will be used as a One-To-One relationship, assuming that the Person has only one phone.
  3. Friends - an entity that will keep all the Personโ€™s friends. It will be used as a One-To-Many relationship, assuming that the person has more than one friend.

As you can see in the above screenshot, I have already created the relationships. I will now explain to you how to that properly (itโ€™s quite straightforward).๐Ÿ‘‡

One-To-One Relationship (Person ->ย Phone)

If you have created the Entities we can proceed with creating the relationship between Person and Phone. You will need to add 3 values in order to create a relationship.

  1. Relationship - name your relationship.
  2. Destination - add the entity you want to establish a relationship with (in our case Phone).
  3. Inverse - create an inverse relationship from Phone and pick it under Person. Apple recommends that you always add an inverse value, so never leave this empty.
Part 1ย (Person)
Part 2ย (Phone)

Code

Each Entity contains its own automatically generated NSManagedObject that you can work within the code. This is one of the advantages of Core Data before others.

Here is an example how you can write in Person and its One-To-One Relationship (Phone).๐Ÿ‘‡

Simple isnโ€™t it?

One-To-Many Relationship (Person ->ย Friends)

I hope that by far you understood how relationships work. Now we will go further and create a One-To-Many relationship. The concept is the same as the One-To-One relationship, just with some minor changes.

When creating a One-To-Many relationship, you will have to change the type to To Many from the Data Model Inspector. This isnโ€™t the case with One-To-One because this type is set to To One by default.

Code

Here is an example how you can write in Person and its One-To-Many Relationship (Friends).๐Ÿ‘‡

The NSManagedObject contains generic methods like addToFriends() where you can pass either a Friends object or an array of Friends.

NOTE: The code that you saw in this tutorial is written in the AppDelegate for simplicity and to provide faster tests, due to the predefined context and Core Data save method.

I am using Core Data relationship in my latestย project:

If you have liked my tutorial and it helped you, please ๐Ÿ‘ or share this story so others can find it as well. Cheers!ย ๐Ÿš€

Comments

Signup or Login to Join the Discussion

Tags

Related Stories