close
AlliOSObjective CTutorial

Relationships in CoreData Using Magical Records

relationships-in-core-data-magical-records

After this post I did on Step by Step Core Data with Magical Records I came across the relationships in core data and I wanted to do a demo for that. You’ll learn how to make relationships in CoreData and the apply create, update, delete and read operations on them using CoreData wrapper MagicalRecords. So in this example I’ll make a .xcDatModel with relationships.

Prerequisite:

Step by Step Core Data with Magical Records

Making Data Model:

You can see how to make a data model from attached prerequisite tutorial. So by following that make the following data model with attributes:

Making Relationships:

Relationships are just like attributes, accessed using key-value coding. Remember the data model we created above, a parent can have many children but children can have only one parent. So this is a one-to-many relationship.
By clicking the + icon in relationships you can add a relationship
To add a to many relation ship you need to change the sidebar.
So finally you data model will look like this
This means that a parent can have zero or more children and each child belongs to one parent.
After that make the NSManagedObject Subclasses for both.

Making CoreData Relationships:

Before going to the code have a look at the storyboard for this example:
This is very simple as you can see it is Human Records will have all the records. Add New will add a new human with and add child will add child to that person.
So make the following three classes and assign them to their controllers:
  • MainTableViewController
  • AddEditParentViewController
  • AddEditChildViewController
Open MainTableViewController.h and here you need to import the following:
and make a property
Next open MainTableViewController.m and import
#import “AddEditParentViewController.h” and after @interface declare an NSArray *fetchedChild;
In viewDidLoad make a call to the method which will fetch all the data as [self fetchHumans]; and right after that [self.tableView reloadData];
Do the same in
Great! Now fetch humans
Now this is a part you need to read.
For a child it is must to have a parent.
And every child might have a different parent.
So whenever we want to find a child we have to look for its parent because child name can be same. So let’s make a method where we will find all child of a specific parent as:
This method is pretty simple. NSPredicate is a string sort of thing which is used to pass some conditions to find the data. Here we have used the parent object in child i.e. parent and if it is equal to the parent we have given as parameter then return the array. Array is used because child can be more than one.
Next it’s time for #pragma mark – Table view methods

 

So these methods are doing some pretty generic stuff. But as you can see where we are setting the textLabel of the cell by Children count we are call that method we made to fetch child with specific parent as [self getChildWithParent:parent] and pass it the parent object we have fetched.
Now one last thing left to do in this class. If you remember the prerequisite tutorial we added the update method. So we are going to do the same for this.

Getting the object at current indexpath and passing it to the Parent object in next class pretty kindergarten stuff.

That’s it for this class. Next we will see what will happen when AddEditParentViewController is launched.
So open up AddEditParentViewController.h where you need to import
and add <UITableViewDelegate, UITableViewDataSource> and following are the properties and actions required in this class.
That’s it for this class. Open up AddEditViewController.m and #import “AddEditChildViewController.h” also make an NSArray *fetchedChild;
In viewDidLoad we will check if it’s and edit parent call or add new parent call so add
New we will see if there is any child available for parent. Again if it’s an edit parent call there will be something in parentToAddEdit.
 So same as before add
Here is a good time to add
And again it’s time for tableView methods
We are not implementing the didSelectRowAtIndexPath method. As it is the same so it’s your task to add this method here. Let’s first implement what will happen when addChild button is pressed:
Here as you can see we are checking if it’s an edit call or add new call and it’ll create a new entity or update the current one. Before pushing the view we are assigning parent object in child class to the parent object of parent class and finally we will implement the add Parent Button as

First of all it is checking if there is an edit call or the call to add new, after that it is setting the text fields text to entity attributes and then it is setting the child object in parent to the method by using NSSet of fetched child array.

That’s it for this class. Now only one class left which is pretty straight forward as we are only adding the child in this class.
Open up AddEditChildViewController.h and again import

Now we will assign the following properties in this header class.


As you can see we have one reference to child and one for parent. Now open AddEditChildViewController.m and do nothing.

If you are doing that task of edit child you should check for childToAddEdit in viewDidLoad. After all of this open addButtonPressed method in this class and do the following.

It’s the same as Parent’s class. We are not calling the save here as it’ll be saved in the parentView class. Now run the project and you’ll see the changes.
Chaudhary Talha

The author Chaudhary Talha

I'm an iOS developer by profession and UI/UX designer by passion. All my tutorials are based on my personal experiences and whenever I learn something new I share it with the world.