Timering

Purp

Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate

First of all, in this tutorial we need to create Hobby Entity, and for this we need to change the Core Data model. After that, we can create and save hobbies to users. Finally, we learn to use SortDescriptor and NSPredicate.

Steps in part 4:

  1. Create Hobby Entity - change Core Data model
  2. Create and Save hobbies to user
  3. SortDescriptor and NSPredicate

Part of the series:

  1. “You must to know!” Collection - Core Data simple explanations
  2. Core Data tutorial Part 1 / 5 - Create Managed Object Model with Attributes, Relationships & Delete Rules
  3. Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
  4. Core Data tutorial Part 3 / 5 - Edit, Delete NSManagedObjects
  5. Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder TableViewCells with LongPressGesture
  6. CoreData Tutorial Part 5/5 - Model Versioning, Lightweight Migration, Model Mapping

iCloud Sync tutorials:

  1. iCloud Sync tutorial Part 1/2 - create iCloudStore, check if user is Signed in, migrate store
  2. iCloud Sync tutorial Part 2/2 - soon!

Video tutorial: 

1. Create Hobby Entity - change Core Data model

1.1. Add New Entity 

Set Name: “Hobby”, and add the attribute: “hobbyName” as String, and then create a relationship: “userofhobby” destination:User,  type: to One.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_1_1

1.2. Add the “hoobiesofuser” relationship to the User entity

Set the Destination to Hobby, set the inverse, as “userofhobby”, finally set type to “to many”.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_1_2

1.3. Now, you must to update your Core Data model. 

You have two ways to do this.

  1. Case: you should just delete the entity’s earlier .h and .m files, and delete the App from the simulator. In this case, you just work on the project, but this App not available in the App Store, so you do the changes just for yourself. We do now this.
  2. Case: you have to do migration, before sending the App to the Appstore. We will do in the fifth part of this series.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_1_3_2_1

So just Delete (Move to trash), and then Editor - Create managedObject again

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_1_3_2_2

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_1_3_2_3

1.4. Open Simulator and delete the App. 

After that run the app, test that it works again. Create some user with type, and save them.
Great!

Probably you will find these line there:

2. Create and Save hobbies to user.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_2

2.1. Create a new TextField 

Create it on storyboard, check delegate!, and declare in ViewController.h file property, and synthesize in .m file, and import Hobby.h file in your ViewCotnroller.m.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_2_1_1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_2_1_2

2.2. Set the Textfield3 properties as the TextField1 and 2. 

You should just copy them. Looks like this:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_2_2_1

2.3. Change the prepareForSegue code to save the hobby for the user. 

Set hobby1 string with TextField3.text, and add to the user.

2.4. Set TableView cell TextLabel with typeNAme and hobbyName string - detect objects

In MyTableViewcontroller.m import Hobby.h file, set the cell.detailTextLabel.text to show at first place the typeName string, and the second place the hobbyName string. You have to create an Array of the user1’s hobbies, and then from that detect hobby1 object, and use the hobbyName string.

2.5. Test you App!

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_2_5

You can see (null), where no hobbies of the user. So you can create a Predicate, to check if user has hobby we list, and in other case no.

Probably you will find these line there:

3. SortDescriptor and NSPredicate

First of all do look at this SortDescriptor code, we use it, and then we can see our list just in alphabetical.

3.1. Put in your AppDelegate.m file in the (NSArray*)getAllUserRecords code:

After the NSEntityDescriptions and before the NSError line this 2 line:

3.2. Run your simulator

You can see, that your list ordered alphabetical. You can test with one user which start with “a”, and one user which start with “z”.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_3_2

3.3. Use NSPredicate to check, that user has hobby or not. 

And if not, we don’t list them. Use this code after your SortDescriptor:

In this case, you see in your simulator just those users, who have no hobby, exactly have a hobby “null” value. You created these users, at the 1.4., when we have no hobby entity.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_3_3_1

If you change the count value from 1 to 0: (hobbiesofuser.@count != 0), you will see those users, who have at least one hobby. Maybe you leave the TextField blank, in this case, the hobby value will be @“”, but not “(null)”. So the count of the hobbiesofuser relationship will be 1, and your user will be list. So when you use 0 in Predicate, you would like not to show those, who has 0 hobby. I did some test without entering hobby in textField3. My result:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_3_3_2

3.4. You can use multiple conditions using “AND” between conditions, and you can use NSPredicate to filter your array.

For example, we would like to see just those users, who’s name hasn’t contained “user” string, we can use this predicate code in your MTableViewcontroller.m file, in cellForRowAtindexpath before this line: User *user1=[self.myArray objectAtIndex:indexPath.row];

And the result: list without username, which contains “user”.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 4 | SortDescriptor 4_3_3_3

Download:

Download the Xcode template from Github.

NEXT

The next part of the series:

Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder/Moving Table View Cells with Long Press Gesture, and update immediately Core Data displayOrder attribute.

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on Facebook or Twitter! Check Footer section for these opportunities!