Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Macjon/3249855 to your computer and use it in GitHub Desktop.

Select an option

Save Macjon/3249855 to your computer and use it in GitHub Desktop.
NSManagedObjectContext for iOS 4. Maybe?
//
// ThreadedManagedObjectContext.h
// Tweetopolis
//
// Created by Daniel Tull on 25.07.2012.
// Copyright (c) 2012 Daniel Tull. All rights reserved.
//
#import <CoreData/CoreData.h>
@interface ThreadedManagedObjectContext : NSManagedObjectContext
- (void)performBlock:(void(^)())block;
- (void)performBlockAndWait:(void(^)())block;
@end
//
// ThreadedManagedObjectContext.m
// Tweetopolis
//
// Created by Daniel Tull on 25.07.2012.
// Copyright (c) 2012 Daniel Tull. All rights reserved.
//
#import "ThreadedManagedObjectContext.h"
@implementation ThreadedManagedObjectContext {
__strong NSOperationQueue *_operationQueue;
}
- (id)init {
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue setMaxConcurrentOperationCount:1];
__block ThreadedManagedObjectContext *myself = self;
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
myself = [super init];
}];
[operationQueue addOperation:blockOperation];
[blockOperation waitUntilFinished];
self = myself;
if (!self) return nil;
_operationQueue = operationQueue;
return self;
}
- (void)performBlock:(void(^)())block {
[_operationQueue addOperationWithBlock:block];
}
- (void)performBlockAndWait:(void(^)())block {
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:block];
[_operationQueue addOperation:blockOperation];
[blockOperation waitUntilFinished];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment