Bulk Deletes

The API for handling bulk deletes was added to Core Data after bulk updates. Therefore, the API is very similar.

 -​ (​void​)deleteOldRecipes
 {
  NSManagedObjectContext *moc = [self writerContext];
  [moc performBlock:^{
  NSDate *yearOld = [self dateFrom1YearAgo]​;
  NSBatchDeleteRequest *request = nil​;
  NSFetchRequest *fetch = nil​;
  NSBatchDeleteResult *result = nil​;
  NSPredicate *predicate = nil​;
  NSError *error = nil​;
 
  fetch = [NSFetchRequest fetchRequestWithEntityName:​@"Recipe"​]​;
  predicate = [NSPredicate predicateWithFormat:​@"lastUsed <= %@"​, yearOld]​;
  [fetch setPredicate:predicate]​;
  request = [[NSBatchDeleteRequest alloc] initWithFetchRequest:fetch]​;

Get Core Data in Objective-C, 3rd Edition 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.