Skip to content

Commit

Permalink
Added method to truncate all objects matching a predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
myell0w committed Apr 2, 2011
1 parent 741adf1 commit b0ea296
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions activerecord-coredata/NSManagedObject+ActiveRecord.h
Expand Up @@ -27,6 +27,7 @@
- (BOOL) deleteInContext:(NSManagedObjectContext *)context;

+ (BOOL) truncateAll;
+ (BOOL) truncateAllMatchingPredicate:(NSPredicate *)searchTerm;
+ (BOOL) truncateAllInContext:(NSManagedObjectContext *)context;

+ (NSArray *) ascendingSortDescriptors:(id)attributesToSortBy, ...;
Expand Down
21 changes: 19 additions & 2 deletions activerecord-coredata/NSManagedObject+ActiveRecord.m
Expand Up @@ -496,7 +496,7 @@ + (NSArray *)findAllWithPredicate:(NSPredicate *)searchTerm withBlock:(void (^)(


+ (id)findFirstByUID:(id)searchValue {
return [self findFirstByUID:searchValue inContext:[NSManagedObjectContext defaultContext]];
return [self findFirstByUID:searchValue inContext:[NSManagedObjectContext defaultContext]];
}

+ (id)findFirstByUID:(id)searchValue inContext:(NSManagedObjectContext *)context {
Expand All @@ -513,7 +513,7 @@ + (id)findFirstByUID:(id)searchValue inContext:(NSManagedObjectContext *)context
}

+ (id)findFirstByUIDHash:(id)searchValue {
return [self findFirstByUID:searchValue inContext:[NSManagedObjectContext defaultContext]];
return [self findFirstByUID:searchValue inContext:[NSManagedObjectContext defaultContext]];
}

+ (id)findFirstByUIDHash:(id)searchValue inContext:(NSManagedObjectContext *)context {
Expand Down Expand Up @@ -685,6 +685,23 @@ + (BOOL) truncateAllInContext:(NSManagedObjectContext *)context
return YES;
}

+ (BOOL) truncateAllMatchingPredicate:(NSPredicate *)searchTerm
{
NSFetchRequest *request = [self requestAllInContext:context];
[request setPredicate:searchTerm];
[request setIncludesSubentities:NO];
[request setIncludesPropertyValues:NO];
[request setFetchBatchSize:[self defaultBatchSize]];

NSArray *objectsToTrucate = [self executeFetchRequest:request];

for (id objectToTruncate in objectsToTrucate) {
[objectsToTrucate deleteEntity];
}

return YES;
}

+ (BOOL) truncateAll
{
[self truncateAllInContext:[NSManagedObjectContext defaultContext]];
Expand Down

0 comments on commit b0ea296

Please sign in to comment.