Skip to content

Instantly share code, notes, and snippets.

@cfr
Last active December 19, 2015 15:59
Show Gist options
  • Save cfr/5980474 to your computer and use it in GitHub Desktop.
Save cfr/5980474 to your computer and use it in GitHub Desktop.
sequence
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