Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSManagedObject helpful additions
#import <CoreData/CoreData.h>
@interface NSManagedObject (Additions)
#pragma mark - Life cycle management
+ (id)createInContext:(NSManagedObjectContext *)context;
- (void)deleteInContext:(NSManagedObjectContext *)context;
#pragma mark - Fetching
+ (NSArray *)fetchInContext:(NSManagedObjectContext *)context;
+ (NSArray *)fetchWithFetchRequest:(NSFetchRequest *)fetchRequest
inContext:(NSManagedObjectContext *)context;
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context;
+ (NSArray *)fetchWithSortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors
inContext:(NSManagedObjectContext *)context;
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors
inContext:(NSManagedObjectContext *)context;
#pragma mark - Counting
+ (NSInteger)countInContext:(NSManagedObjectContext *)context;
+ (NSInteger)countWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context;
+ (NSInteger)countForFetchRequest:(NSFetchRequest *)fetchRequest
inContext:(NSManagedObjectContext *)context;
#pragma mark - Existing
+ (BOOL)existInContext:(NSManagedObjectContext *)context;
+ (BOOL)existWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context;
#pragma mark - Fetch request management
+ (NSFetchRequest *)fetchRequestForCurrentEntity;
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate;
+ (NSFetchRequest *)fetchRequestWithSortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors;
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors;
@end
#import "NSManagedObject+Additions.h"
@implementation NSManagedObject (Additions)
#pragma mark - Life cycle management
+ (id)createInContext:(NSManagedObjectContext *)context {
return [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass(self)
inManagedObjectContext:context];
}
- (void)deleteInContext:(NSManagedObjectContext *)context {
[context deleteObject:self];
}
#pragma mark - Fetching
+ (NSArray *)fetchInContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestForCurrentEntity];
return [self fetchWithFetchRequest:fetchRequest inContext:context];
}
+ (NSArray *)fetchWithFetchRequest:(NSFetchRequest *)fetchRequest
inContext:(NSManagedObjectContext *)context {
NSError *__autoreleasing error = nil;
NSArray *objects = [context executeFetchRequest:fetchRequest
error:&error];
return objects;
}
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestWithPredicate:predicate];
return [self fetchWithFetchRequest:fetchRequest
inContext:context];
}
+ (NSArray *)fetchWithSortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors
inContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestWithSortDescriptors:sortDescriptors];
return [self fetchWithFetchRequest:fetchRequest
inContext:context];
}
+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors
inContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestWithPredicate:predicate
sortDescriptors:sortDescriptors];
return [self fetchWithFetchRequest:fetchRequest
inContext:context];
}
#pragma mark - Counting
+ (NSInteger)countInContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestForCurrentEntity];
return [self countForFetchRequest:fetchRequest
inContext:context];
}
+ (NSInteger)countWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestWithPredicate:predicate];
return [self countForFetchRequest:fetchRequest
inContext:context];
}
+ (NSInteger)countForFetchRequest:(NSFetchRequest *)fetchRequest
inContext:(NSManagedObjectContext *)context {
NSError *__autoreleasing error = nil;
return [context countForFetchRequest:fetchRequest error:&error];
}
#pragma mark - Existing
+ (BOOL)existInContext:(NSManagedObjectContext *)context {
return [self existWithPredicate:nil inContext:context];
}
+ (BOOL)existWithPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [self fetchRequestForCurrentEntity];
fetchRequest.predicate = predicate;
fetchRequest.fetchLimit = 1;
NSInteger count = [self countForFetchRequest:fetchRequest inContext:context];
return (count > 0);
}
#pragma mark - Fetch request management
+ (NSFetchRequest *)fetchRequestForCurrentEntity {
return [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass(self)];
}
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate {
return [self fetchRequestWithPredicate:predicate sortDescriptors:nil];
}
+ (NSFetchRequest *)fetchRequestWithSortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors {
return [self fetchRequestWithPredicate:nil sortDescriptors:sortDescriptors];
}
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray <NSSortDescriptor*> *)sortDescriptors {
NSFetchRequest *fetchRequest = [self fetchRequestForCurrentEntity];
fetchRequest.predicate = predicate;
fetchRequest.sortDescriptors = sortDescriptors;
return fetchRequest;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment