AFNetworking with block callback

We all know AFNetworking fetching data asynchronously.
Usually it convenient to do things inside completion block, but in some situations we want some more.

  • We have to ask it for something when the data is downloaded.
  • Unit testing usage.
  • Better code readability.

There's a common philosophy as the author of AFNetworing Mattt Thompson said.

The trick to asynchronous programming is to break the procedural, synchronous assumption that data is there when you ask for it. Instead, with async, when you ask for something, you give it a callback to perform when the data finally is ready.

So, implement a block callback into completion block is what we need to do.

Assume that we have a Connection Class, fetching data down, and call another block when data downloaded.

@interface Connection : NSObject

+ (void)fetchAPI:(void (^)(id json))parser;

@end
@implementation Connection

+ (void)fetchAPI:(void (^)(id json))parser
{
    NSURL *url = [NSURL URLWithString:SODAPI];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id obj){
        parser(obj);
    }failure:nil];

    [operation start];
}

Somewhere we really use it.

    [Connection fetchAPI:^(id json) {
        NSLog(@"data : %@", json);
    }];
comments powered by Disqus