Last active
August 29, 2015 14:14
-
-
Save lerno/df885dbd78a3f1f93f72 to your computer and use it in GitHub Desktop.
This file contains 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
// Synchronous | |
@implementation A | |
{ | |
B *b; | |
} | |
- (void)doSomething { | |
/* ... */ | |
BOOL x = [b performOne]; | |
if (x) [b performTwo]; | |
} | |
@end | |
@interface B : NSObject | |
- (BOOL)performOne; | |
- (void)performTwo; | |
@end | |
// Making this asynchronous | |
@interface AsyncThread : NSObject | |
- (void)executeAsync(Block block); // Execute in the thread associated with this object | |
@end | |
// Block style #1: | |
@interface B : AsyncThread | |
- (BOOL)performOne; | |
- (void)peformTwo; | |
@end | |
@implementation A | |
{ | |
B *b; | |
} | |
- (void)doSomething { | |
/* ... */ | |
[b executeAsync:^{ | |
BOOL x = [b performOne]; | |
if (x) [b performTwo]; | |
}]; | |
} | |
// Block style #2 | |
@interface A : AsyncThread | |
@interface B : AsyncThread | |
- (void)performOneMaybeTwo; // performOne performTwo no longer exposed externally. | |
@end | |
@implementation A | |
{ | |
B *b; | |
} | |
- (void)doSomething { | |
/* ... */ | |
[b executeAsync:^{ | |
[b performOneMaybeTwo]; | |
}]; | |
} | |
@end | |
@implementation B | |
- (void)performOneMaybeTwo { | |
BOOL x = [self performOne]; | |
if (x) [self performTwo]; | |
} | |
@end | |
// With messages instead: | |
@interface AsyncThread2 | |
- (void)executeAsync:(SEL)selector withObject:(id)arg; // Execute on object thread | |
@end | |
@interface A : AsyncThread2 | |
@interface B : AsyncThread2 | |
- (void)performOneMaybeTwo; | |
@end | |
@implementation A | |
{ | |
B *b; | |
} | |
- (void)doSomething { | |
/* ... */ | |
[b executeAsync:@selector(performOneMaybeTwo) withObject:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment