Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSManagedObjectContext helpful additions for async request execution
#import <CoreData/CoreData.h>
@interface NSManagedObjectContext (AsyncExecute)
- (void)asyncExecuteFetchRequest:(NSFetchRequest *)request
completion:(void (^)(NSArray *objects, NSError *error))completion;
- (void)asyncCountForFetchRequest:(NSFetchRequest *)request
completion:(void (^)(NSInteger count, NSError *error))completion;
- (void)asyncExecuteBlockInBackgroundContext:(void (^)(NSManagedObjectContext *backgroundContext))block;
@end
#import "NSManagedObjectContext+AsyncExecute.h"
@implementation NSManagedObjectContext (AsyncExecute)
- (void)asyncExecuteFetchRequest:(NSFetchRequest *)request
completion:(void (^)(NSArray *objects, NSError *error))completion {
NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
NSFetchRequestResultType resultType = [request resultType];
NSManagedObjectContext *backgroundContext = [NSManagedObjectContext privateQueueManagedObjectContext];
[backgroundContext performBlock:^{
backgroundContext.persistentStoreCoordinator = coordinator;
NSError *error = nil;
NSArray *fetchedObjects = [backgroundContext executeFetchRequest:request error:&error];
[self performBlock:^{
if (resultType == NSManagedObjectResultType && fetchedObjects) {
NSArray *objectIds = [fetchedObjects valueForKey:@"objectID"];
NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:[objectIds count]];
for (NSManagedObjectID * objectID in objectIds) {
NSManagedObject * obj = [self objectWithID:objectID];
[objects addObject:obj];
}
if (completion) {
completion([objects copy], nil);
}
}
else {
if (completion) {
completion(fetchedObjects, error);
}
}
}];
}];
}
- (void)asyncCountForFetchRequest:(NSFetchRequest *)request
completion:(void (^)(NSInteger count, NSError *error))completion {
NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
NSManagedObjectContext *backgroundContext = [NSManagedObjectContext privateQueueManagedObjectContext];
[backgroundContext performBlock:^{
backgroundContext.persistentStoreCoordinator = coordinator;
NSError *error = nil;
NSInteger count = [backgroundContext countForFetchRequest:request error:&error];
[self performBlock:^{
if (completion) {
completion(count, error);
}
}];
}];
}
- (void)asyncExecuteBlockInBackgroundContext:(void (^)(NSManagedObjectContext *backgroundContext))block {
NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
NSManagedObjectContext *backgroundContext = [NSManagedObjectContext privateQueueManagedObjectContext];
[backgroundContext performBlock:^{
backgroundContext.persistentStoreCoordinator = coordinator;
if (block) {
block(backgroundContext);
}
}];
}
#pragma mark - Helpers
+ (instancetype)privateQueueManagedObjectContext {
return [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment