Timering

Purp

Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest

Our goal to save User Entity’s Name (userName-string) and the type of the user, so the Type Entity’s Name (typeName-string), and FetchRequest in UITableViewController the user name and the type Relationship’s Name attribute.

Steps:

  1. ViewController settings - UITextFields, UIButton, TableViewController, Show Segue 
  2. Set property NSManagedObjectModel, NSManagedObjectContext and NSPersistenStoreCoordinator
  3. Save User Entity’s data and Relationships in ViewController.m file
  4. FetchRequest in TableViewController.m - List users with Type relationship’s attribute
  5. Test your App

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 3 / 5 - Edit, Delete NSManagedObjects
  4. Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate
  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. ViewController settings - UITextFields, UIButton, TableViewController, Show Segue

1.1. UITextFields, UIButton and IBOutlet

First create 2 TextField, 1 Button on your View, and create IBOutlet in your .h file, and implement <UITextFieldDelegate>…

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_1_1

Then click on one of the UITextFields, and in Connection Inspector Ctrl-drag “delegate” and connect to the ViewController (yellow icon). Do the same with the other one.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_1_2

1.2. synthesize the IBOutlets in your ViewController.m file, and put the UITextField codes codes:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_2_1

- (void)textFieldDidBeginEditing:(UITextField *)textField
// When start to edit the placeholder will disappear - (void)textFieldDidEndEditing:(UITextField *)textField
//When end editing - which text show in the TexField placeholder -(BOOL)textFieldShouldReturn:(UITextField *)textField{
//Keyboard Return button - end editing, dismiss keyboard -(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
// If you touch outside of TextField your editing will stop and keyboard dismiss

1.3. Create a New TableViewController, and create MyTableViewController subclass

1.3.1. Drag&drop the TableViewController from Object library, use Simulated Metrics size refer to one of iPhone sizes:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_1

1.3.2. Create MyTableViewController.h and .m file so right click - new File - Cocoa Touch class - Type: UITableViewController - Name: MyTableViewController

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_2_1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_2_2

1.3.3. AddMyTableViewController subclass to your new TableViewController on your storyboard…

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_3_1

… you can use the same name as Storyboard ID …

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_3_2

….click on PrototypeCell and set cell Reuse Identifier as “Cell”…

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_3_3

… and create Show segue from myButton to MyTableViewController, and set Identifier: “ListUsersSegue”

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_3_3_4

1.4. Embed NavigationController

Click on First ViewController, Editor - Embed in - NavigationController, and use Simulated Metrics again.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_4_1

Finally, your Main storyboard looks like this:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 1_4_2

Probably you will find these line there:

2. Set property NyManagedObjectModel, NSManagedObjectContext and NSPersistenStoreCoordinator

in ViewController.m file import “AppDelegate.h” file, and copy:
properties from AppDelegate.h to ViewController.h, and synthesize from AppDelegate.m to ViewController.m.

2.1. Do the same with MyTableViewController.h and .m file

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 2_1

2.2. Core Data ManagedObjectContext and PersistentStoreCoordinator - “You must to know!”

Check my article: “You must to know! Collection - Core Data simple explanations” 1. and 2. part

3. Save User Entity’s data in ViewController.m file

3.1. in ViewController.m file import MyTableViewController.h, User.h and Type.h files

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 3_1

3.2. put prepareForSegue: 

Code to your ViewController.m. So we save attributes before the TableViewController appears.

3.3. in prepareForSegue set user attributes to save

Probably you will find these line there:

4. FetchRequest in TableViewController.m - List users with type relationship attribute

4.1. Core Data FetchRequest, NSPredicate, NSSortDescriptor - “You must to know!”

Check my article: “ You must to know! Collection - Core Data simple explanations ” 5. part

4.2. Create NSArray FetchRequest in your AppDelegate.m file before @end to request all User objects:

call it in you AppDelegate.h file:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 4_2

4.3. MyTableViewController FetchRequest

Create an Array in your MyTableViewController.h file, and then synthesize in .m file and in viewDidLoad call the AppDelegate AllUser fetch request.
So your myArray is actually your users group. Now your ViewController.h and m file looks like this:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 4_3

4.4. Set TableView details, so

  • - numberOfSectionsInTableView:
  • - numberOfRowsInSection:
  • - cellForRowAtIndexPath:

4.5. CoreData SQL Debug:

Debug Core Data - Put this code into Product - Scheme - Edit Scheme menu: Run

-com.apple.CoreData.SQLDebug 1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 4_5_1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 4_5_2

Now, you will see the save and fetch process log.

5.Test your App

Maybe you can set in viewDidLoad this:

self.TextField1.placeholder = @”I am userName”; self.TextField2.placeholder = @”I am typeName”;

And the result:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 2 | Save Entity 5

If you would like to be notified of my new solutions, please subscribe to my youtube channel and/ or follow me on twitter!

Download:

 Download the Xcode template from Github.

NEXT

The next part of the series: Core Data tutorial Part 3 / 5 - Edit, Delete NSManagedObjects

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!