Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter asynchronous control flow with Objective-C blocks
    primarykey
    data
    text
    <p>I'm using <a href="https://github.com/AFNetworking/AFNetworking">AFNetworking</a> for asynchronous calls to a web service. Some of these calls must be chained together, where the results of call A are used by call B which are used by call C, etc.</p> <p>AFNetworking handles results of async calls with success/failure blocks set at the time the operation is created:</p> <pre><code>NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Public Timeline: %@", JSON); } failure:nil]; [operation start]; </code></pre> <p>This results in nested async call blocks which quickly becomes unreadable. It's even more complicated when tasks are not dependent on one another and instead must execute in parallel and execution depends on the results of all operations.</p> <p>It seems that a better approach would be to leverage a <a href="http://en.wikipedia.org/wiki/Futures_and_promises">promises</a> framework to clean up the control flow.</p> <p>I've come across <a href="https://github.com/mikeash/MAFuture">MAFuture</a> but can't figure out how best to integrate it with AFNetworking. Since the async calls could have multiple results (success/failure) and don't have a return value it doesn't seem like an ideal fit. </p> <p>Any pointers or ideas would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload