Skip to content

Instantly share code, notes, and snippets.

@benvium
Created February 13, 2015 14:41
Show Gist options
  • Select an option

  • Save benvium/ac98138776ff19d0fea4 to your computer and use it in GitHub Desktop.

Select an option

Save benvium/ac98138776ff19d0fea4 to your computer and use it in GitHub Desktop.
Delete all instances of an NSManagedObject from CoreData.
@import CoreData;
#import <Foundation/Foundation.h>
@interface NSManagedObject (CALDeleteAll)
/**
* Deletes all instances of this object from CoreData (may take a while)
*/
+ (void)cal_deleteAll:(NSManagedObjectContext *)context;
@end
#import "NSManagedObject+CALDeleteAll.h"
@implementation NSManagedObject (CALDeleteAll)
+ (void)cal_deleteAll:(NSManagedObjectContext *)context {
NSFetchRequest *allRecords = [[NSFetchRequest alloc] init];
[allRecords setEntity:[NSEntityDescription entityForName:NSStringFromClass(self.class)
inManagedObjectContext:context]];
[allRecords setIncludesPropertyValues:NO];
NSError *error = nil;
NSArray *result = [context executeFetchRequest:allRecords error:&error];
for (NSManagedObject *profile in result) {
[context deleteObject:profile];
}
NSError *saveError = nil;
[context save:&saveError];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment