Last active
December 19, 2015 15:59
-
-
Save cfr/5980474 to your computer and use it in GitHub Desktop.
sequence
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
typedef void (^OpCompletionEither)(BOOL success); | |
- (void)sequence:(NSArray *)selectors | |
withFinalBlock:(OpCompletionEither)block { | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | |
__block OpCompletionEither _block = block; | |
SEL selector = NSSelectorFromString(selectors[0]); | |
if (selectors.count == 1) | |
// last request, perform reply block | |
[self performSelector:selector withObject:_block]; | |
else | |
[self performSelector:selector withObject:^(BOOL success) { | |
// iterate on tail | |
NSRange tailRange = {1, selectors.count - 1}; | |
[self sequence:[selectors subarrayWithRange:tailRange] | |
withFinalBlock:_block]; | |
}]; | |
#pragma clang diagnostic pop | |
} | |
/* usage */ | |
[self sequence:@[NSStringFromSelector(@selector(getCitiesWithBlock:)), | |
NSStringFromSelector(@selector(getSubwaysWithBlock:))] | |
withFinalBlock:^(BOOL success) { | |
NSLog(@"%@", success ? @"ok" : @"fail"); | |
}]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment