Skip to content

Instantly share code, notes, and snippets.

@asparagui
Created March 18, 2014 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asparagui/9628651 to your computer and use it in GitHub Desktop.
Save asparagui/9628651 to your computer and use it in GitHub Desktop.
- (void) deleteAllObjects: (NSString *) entityDescription {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = [self backgroundManagedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *managedObject in items) {
[managedObjectContext deleteObject:managedObject];
//NSLog(@"%@ object deleted",entityDescription);
}
if (![managedObjectContext save:&error]) {
//NSLog(@"Error deleting %@ - error:%@",entityDescription,error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment