Forked from danielctull/ThreadedManagedObjectContext.h
Created
August 3, 2012 17:36
-
-
Save Macjon/3249855 to your computer and use it in GitHub Desktop.
NSManagedObjectContext for iOS 4. Maybe?
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
| // | |
| // 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 |
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
| // | |
| // 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