Clear Core Data Table


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. - (void) deleteAllObjects: (NSString *) entityDescription {
  2. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  3. NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext];
  4. [fetchRequest setEntity:entity];
  5.  
  6. NSError *error;
  7. NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
  8. [fetchRequest release];
  9.  
  10.  
  11. for (NSManagedObject *managedObject in items) {
  12. [_managedObjectContext deleteObject:managedObject];
  13. NSLog(@"%@ object deleted",entityDescription);
  14. }
  15. if (![_managedObjectContext save:&error]) {
  16. NSLog(@"Error deleting %@ - error:%@",entityDescription,error);
  17. }
  18.  
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.