Created
February 13, 2015 14:41
-
-
Save benvium/ac98138776ff19d0fea4 to your computer and use it in GitHub Desktop.
Delete all instances of an NSManagedObject from CoreData.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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