About a month ago I mentioned a Backbone.js inspired example for managing UIViews in Objective-C from Tal Bereznitsky.
Here’s a handy little library called Sequencer from Tal that provides a very neat way to manage asynchronous flow control.
As the readme states:
Sequencer turns complicated nested blocks logic into a clean, straightforward, and readable code.
Here’s an example from the readme showing how to get the latest feed item from app.net and display the contents of a URL using AFNetworking.
[sequencer enqueueStep:^(id result, SequencerCompletion completion) {
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
completion(JSON);
} failure:nil];
[operation start];
}];
[sequencer enqueueStep:^(NSDictionary *feed, SequencerCompletion completion) {
NSArray *data = [feed objectForKey:@"data"];
NSDictionary *lastFeedItem = [data lastObject];
NSString *cononicalURL = [lastFeedItem objectForKey:@"canonical_url"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:cononicalURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(responseObject);
} failure:nil];
[operation start];
}];
[sequencer enqueueStep:^(NSData *htmlData, SequencerCompletion completion) {
NSString *html = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
NSLog(@"HTML Page: %@", html);
completion(nil);
}];
[sequencer run];
You can find Sequencer on Github here.
A nice simple library for asynchronous flow control.
- Drop-In UISegmentedControl Replacement That Mimics Look Of iOS 6 App Store Tabs
- Open Source: Library For Easy USB And TCP Communication Within iOS/Mac Apps
- Open Source: Communication Libraries For Easy In iOS App HTTP Servers And Asynchronous UDP/TCP Communication
- iOS AFNetworking Add-On Library That Provides Accelerated Downloading
- Best Resources In iOS Development – July 30th, 2012
Original article: Open Source library For Easy iOS Objective-C Asynchronous Flow Control
©2013 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.