Last active
August 29, 2015 13:57
-
-
Save vfleurima/9423407 to your computer and use it in GitHub Desktop.
A solution to the question posted in http://inessential.com/2014/03/07/async_startup_and_locking using NSProxy.
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
| #import <Foundation/Foundation.h> | |
| @protocol AsyncInit<NSObject> | |
| -(void)performAsyncInit; | |
| -(dispatch_queue_t)serial_queue; | |
| @end | |
| @interface AsyncInitProxy : NSProxy | |
| @property id<AsyncInit> forwardedObject; | |
| -(id)initWithForwardedObject:(id<AsyncInit>)forwardedObject; | |
| @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
| #import "AsyncInitProxy.h" | |
| @implementation AsyncInitProxy | |
| -(id)initWithForwardedObject:(id<AsyncInit>)forwardedObject | |
| { | |
| self.forwardedObject = forwardedObject; | |
| return self; | |
| } | |
| -(NSMethodSignature *)methodSignatureForSelector:(SEL)sel | |
| { | |
| return [(NSObject *)_forwardedObject methodSignatureForSelector:sel]; | |
| } | |
| -(void)forwardInvocation:(NSInvocation *)invocation | |
| { | |
| dispatch_sync([_forwardedObject serial_queue], ^{ | |
| [invocation setTarget:_forwardedObject]; | |
| [invocation invoke]; | |
| }); | |
| } | |
| @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
| #import <Foundation/Foundation.h> | |
| #import "Worker.h" | |
| int main(int argc, const char * argv[]) | |
| { | |
| @autoreleasepool | |
| { | |
| NSDate *startTime; | |
| NSTimeInterval timeElapsed; | |
| Worker *worker = [[Worker alloc] init]; | |
| startTime = [NSDate date]; | |
| [worker meaninglessOperation1]; | |
| timeElapsed = [[NSDate date] timeIntervalSince1970] - [startTime timeIntervalSince1970]; | |
| NSLog(@"Time elapsed during meaninglessOperation1: %.2f seconds", timeElapsed); | |
| startTime = [NSDate date]; | |
| [worker meaninglessOperation2]; | |
| timeElapsed = [[NSDate date] timeIntervalSince1970] - [startTime timeIntervalSince1970]; | |
| NSLog(@"Time elapsed during meaninglessOperation2: %.2f seconds", timeElapsed); | |
| } | |
| return 0; | |
| } |
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
| #import <Foundation/Foundation.h> | |
| @interface Worker : NSObject | |
| -(void)meaninglessOperation1; | |
| -(void)meaninglessOperation2; | |
| @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
| #import "Worker.h" | |
| #import "AsyncInitProxy.h" | |
| @interface Worker () <AsyncInit> | |
| @property (nonatomic, strong) dispatch_queue_t serial_queue; | |
| @end | |
| @implementation Worker | |
| -(id)init | |
| { | |
| if (self = [super init]) { | |
| self.serial_queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); | |
| [self performAsyncInit]; | |
| AsyncInitProxy *proxy = [[AsyncInitProxy alloc] initWithForwardedObject:self]; | |
| return (Worker *)proxy; | |
| } else { | |
| return self; | |
| } | |
| } | |
| -(void)performAsyncInit | |
| { | |
| dispatch_async(self.serial_queue, ^{ | |
| sleep(5); | |
| }); | |
| } | |
| -(void)meaninglessOperation1 | |
| { | |
| NSLog(@"foo"); | |
| } | |
| -(void)meaninglessOperation2 | |
| { | |
| NSLog(@"bar"); | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment