Deleting all entities
February 27, 2013 in iOS Snippets
The following code simply fetches and deletes all objects of a given entity name – just remember to save the changes afterwards.
+ (void)deleteAllEntities:(NSString*)name inContext:(NSManagedObjectContext*)context;
{
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:name inManagedObjectContext:context]];
NSArray* fetchResult = [context executeFetchRequest:fetchRequest error:nil];
for (id obj in fetchResult) {
[context deleteObject:obj];
}
}