Timering

Purp

Core Data tutorial Part 3 / 5 - Edit, Delete NSManagedObjects

In this tutorial, we edit user data and delete attributes. We save the modified NSManagedObject's attributes in Core Data, and we delete the objects too at the appropriate NSManagedObjectContext. Xcode 6.1, IOS 8.1 tutorial. 

Steps:

  1. Edit user1.userName or type1.typeName attributes
  2. Test Your App, run Simulator, edit and save your user attributes
  3. Delete objects
  4. Test your App again, you can now edit user data, and swipe and delete.

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 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. Edit user1.userName or type1.typeName attributes

1.1. in MyTableViewController.h file import “User.h”, create User *selecteduser property, and then synthesize it in .m file

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 3 | Edit and Delete Object 1_1

1.2. do the same in the ViewController.h and .m file

1.3. Create UnwindSegue code in ViewController.m file:

1.4. unwindSegue with PrototypeCell

Ctrl drag&drop the PrototypeCell on the storyboard to the MyTableViewController Exit icon, and choose “unwindFromSegue:”, and then add Segue name: “selectuser”.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 3 | Edit and Delete Object 1_4_1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 3 | Edit and Delete Object 1_4_2

1.5. in MyTableViewController.m file #import “ViewController.h and put the didSelectRowAtIndexPath: code:

Probably you will find these line there:

1.6. in MyTableviewController.m file in prepareForSegue set the selecteduser data in the ViewController.m file with this code:

1.7. in viewController.h file create myArray NSArray, and then synthesize.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 3 | Edit and Delete Object 1_7

1.8. in ViewController.m file in unwindFromSegue:

set the textfield.text with selecteduser’s attributes:

1.9. Create string in ViewController.m file “sameString” 

Synthesize, and set in unwindFromSegue with string:@“same”.

self.sameString=@”same”;

1.10. in ViewController.m in prepareForSegue 

you should do if statement with sameString, and put here the save selecteduser name and  typename code:

Set selected user in didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selecteduser = [self.myArray objectAtIndex:indexPath.row]; [self performSegueWithIdentifier:@“selectuser” sender:self]; }

Finally your prepareForSegue code looks like this:

2. Test Your App, run Simulator, edit and save your user attributes

Check the Video tutorial at this point.

Probably you will find these line there:

3. Delete objects

3.1. in your MyTableViewController.m file in green text find and activate canEditRowAtIndexPath: and commitEditingStyle: codes:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 3 | Edit and Delete Object 3_1

3.2. change the Editing StyleDelete if statement part code to this: 

Because you need to delete object from database too, not just delete the row from the screen:

4. Test your App again, you can now edit user data, and swipe and delete.

Check the Video tutorial at this point.

Download:

Download the Xcode template from Github.

NEXT

The next part of the series: Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate.

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!