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.
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:
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).👇
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.
Part 1 (Person)
Part 2 (Phone)
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?
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_._
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.
1x2 BET - Soccer Tips & Odds_HOT ODDS Each day, we generate a list of the hottest odds in the world. These are odds that have dropped the most…_apple.co