Using Core Data: Workshop 5

So far you’ve set up the Book List and Add Book scenes. You added code in their class files to make them functional. In today’s workshop you’ll add code in the BookListViewController class to enable the user to delete a book from the persistent store and to delete a row from the view’s table view control.

In order to enable the app user to perform above tasks, you’ll have to locate the commitEditingStyle: method in the BookListViewController.m file, then modify its code to what’s shown below.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSArray *bookArray = self.tableData[indexPath.row];
    NSString *bookTitleToDelete = bookArray[0];

    //1. This method delete a book from the persistent store
    [self.appDelegate deleteBook:bookTitleToDelete];

    // Delete a row from the view's table view control
    [self.tableData removeObjectAtIndex:indexPath.row];

    // Refresh the Table View rows
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  } else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  }
}

In above method, you used the deleteBook: method. Here is the code to declare and implement it in the AppDelegate class.

- (void) deleteBook:(NSString *)bookTitle
{
  NSError *error = nil;

  // Create a managed object context object
  NSManagedObjectContext *context = [self managedObjectContext];

  // Create a fetch request object
  NSFetchRequest *request = [[NSFetchRequest alloc] init];

  // Create an entity description object
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:context];

  // Add the entity description
  [request setEntity:entity];

  // Set up the predicate (fetch filter condition)
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@", bookTitle];

  // Add the predicate to the fetch request
  [request setPredicate:predicate];

  // Execute the fetch request
  NSArray *managedObjectsArray = [context executeFetchRequest:request error:&error];

  for (NSManagedObject *object in managedObjectsArray) {
    // Delete the object from the context
    [context deleteObject:object];
  }

  // Save changes made to the context in the persistent store
  if (![context save:&error]) {
    // The fetch request failed, so display the error message it generated
    NSLog(@"%@",error);
  }
}

That’s it, you should run the application and test the commitEditingStyle: method’s code. Delete one or more rows from the table view control, then terminate the app. When you run the app again, the rows you deleted won’t appear in the table view control.

That’s all for this week’s workshop. In next week’s workshop, you will learn how to update a managed object (a book) in the persistent store. Until then, happy coding! 🙂