Skip to content

Instantly share code, notes, and snippets.

@lerno
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lerno/df885dbd78a3f1f93f72 to your computer and use it in GitHub Desktop.
Save lerno/df885dbd78a3f1f93f72 to your computer and use it in GitHub Desktop.
// 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