Deleting Managed Objects

Deleting a managed object is as easy as calling deleteObject or deleteObjects on a containing context. Note that the deletion isn’t permanent until you also call save: on the context. The code involved is shown in Listing 2.9.

Listing 2.9 AppDelegate: demo (Deleting)

NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"Item"];NSArray *fetchedObjects =[_coreDataHelper.context executeFetchRequest:request error:nil];for (Item *item in fetchedObjects) {    NSLog(@"Deleting Object '%@'", item.name);    [_coreDataHelper.context deleteObject:item];}

Update Grocery Dude as follows to delete all the objects:

1. Replace all code in the demo method with the code from Listing 2.9 ...

Get Learning Core Data for iOS: A Hands-On Guide to Building Core Data Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.