Today, you will implement these tasks in the SwiftCoreDataFromScratch project you worked on last week:

  • Delete a managed object from the table view and the app’s database file
  • Refresh the table view cells

Before You Begin

Now, before implementing above tasks in the project, I want you to use the Source Control menu to create a new branch called, “delete-managed-object” from the update-managed-object branch. Once you’ve done that, click the BooksViewController.swift file, because you’ll be adding code in it.

The Code

Click the BooksViewController.swift file and enter code shown below in the commitEditingStyle function.

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    let managedObjectToDelete = fetchedResultsController.objectAtIndexPath(indexPath) as Book
    context.deleteObject(managedObjectToDelete)

    var savingError: NSError?
    if !context.save(&savingError) {
        println("Failed to save the context with error = \(savingError?.localizedDescription)")
    }
}

Next, add these delegate functions of the NSFetchedResultsControllerDelegate protocol in the file:

// MARK: -  NSFetchedResultsController delegate functions

func controllerWillChangeContent(controller: NSFetchedResultsController!) {
  tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
  if type == .Delete {
    tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
  } else if type == .Insert {
    tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
  } else if type == .Update {
    tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
  }
}

func controllerDidChangeContent(controller: NSFetchedResultsController!) {
  tableView.endUpdates()
}

The commitEditingStyle() function is fired when you swipe a cell in the table view. Code you entered in it basically, delete the swiped cell’s managed object from the context and the database file. Now, the three delegate functions of the NSFetchedResultsControllerDelegate protocol is responsible for notifying its delegate, which is the tableView object.

The first function, controllerWillChangeContent() inform the tableView that updates are supposed to occur soon. The second function, didChangeObject() update the table view to reflect the modification made in the context. Code you entered in the function delete the swiped cell from the table view and refresh it so the deleted cell no longer appear in the table view. The third function, controllerDidChangeContent() inform the tableView that the updates are now complete.

Test The Code

Go ahead and run the app in the iPhone 5s Simulator and test the code you entered in the BooksViewController.swift file. Delete one or more cells.

table view after cell deletion occurred

table view after cell deletion occurred

By the way, when you run the app again, cells you deleted won’t appear in the table view. Don’t forget to use the Source Control to commit changes you’ve made to the project, in the git repository.

You’ve reached the end of the fourth workshop in the Swift Core Data From Scratch tutorial. Comments are welcomed. Next week, I will bring you the final workshop.

Tags:

No responses yet

Leave a Reply

UIDocument Demystified: A Step-by-step Guide on Local and iCloud Document Storage

 

Archives