Deleting Events

The goal of this chapter is to allow the user to delete events from the list.

Deleting Managed Objects

As you saw when you created a new managed object, the lifetime of a record in the database is not tied to the lifetime of a given managed object. If you create a managed object, it doesn’t mean a record automatically is created for that object in the database—you need to save the context. Similarly, simply because an object is deallocated does not mean that the corresponding record itself is destroyed.

To delete a record, you tell the managed object context to mark an object as deleted, using the deleteObject: method of NSManagedObjectContext. Then to actually destroy the record, you commit the action using save:.

Deleting an Event

To handle deletion, you implement the table view data source method tableView:commitEditingStyle:forRowAtIndexPath:. It needs to do three things:

  1. Delete the selected object.

  2. Update the table view.

  3. Save the changes.

It should do this only if the action is a delete.

>> In the RootViewController implementation file, implement the tableView:commitEditingStyle:forRowAtIndexPath: method as follows:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
    if (editingStyle == UITableViewCellEditingStyleDelete) {
 
        // Delete the managed object at the given index path.
        NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
        [managedObjectContext deleteObject:eventToDelete];
 
        // Update the array and table view.
        [eventsArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 
        // Commit the change.
        NSError *error = nil;
        if (![managedObjectContext save:&error]) {
            // Handle the error.
        }
    }
}

Build and Test

Build and test the application. You should find that it compiles and runs without error. If you tap Edit, the table view should enter edit mode. If you delete a row, it should be properly deleted from the table view. If you quit and relaunch the application, the row you deleted should no longer be visible.

You’ve now completed the tutorial. You can start investigating ways to enhance your knowledge and understanding of Core Data. Some suggestions are given in the next chapter.

Core Data Recap

You’ve now performed the basic tasks you need to be familiar with to use Core Data—you:

You may have noticed that, in performing these tasks, the only object in the Core Data stack (see “The Core Data Stack”) with which you interacted directly was the managed object context. Although you have access to the other objects in the stack, you often don’t need to use them directly. Either the Xcode template takes care of setting them up, or you use a convenience class method to accomplish a particular task that would otherwise require you to access them.


Did this document help you? Yes It's good, but... Not helpful...