Josh Jones
Josh Jones

Reputation: 498

AFNetworking and Batched/Serial Operations

I'm trying to do a series of network operations with AFNetworking. For example:

[SomeApiICreated doNetworkingStuff success:^(NSString *message)
                           {
                             NSLog(@"Yay!");
                           }
                           failure:^(NSError *error)
                           {
                             NSLog(@"Oh noes!");
                           }];

Now within doNetworkingStuff, I want to do potentially multiple network tasks. For example

  1. Get app token
  2. if (appTokenGot) step3 else callFailure
  3. Get list of stuff using app token
  4. if (listGot) step5 else callFailure
  5. etc.

But i'd like to also call a doNetworkingStuff2 that only does steps 3 and above, basically letting me queue up the operations as I need them, mix and match em, etc. But all with one main call that I can say "Yes this worked", or "there was a problem"

Is the NSOperationQueue the right thing for this, or is a nested function of "if this, then this" blocks the best way to do this?

Any examples would be helpful as well.

Upvotes: 2

Views: 507

Answers (2)

alvaromb
alvaromb

Reputation: 4856

AFAIK, the best way to do this is to use the completion callbacks to do the successive calls. Keep in mind that you need the response object to do the successive steps, so you are receiving this object in the success callback.

Upvotes: 0

Keith Smiley
Keith Smiley

Reputation: 63984

Why not just call other functions passing the data from within the success/failure blocks. For example I use something like this with the App.net API.

[[KSADNAPIClient sharedAPI] postPath:@"stream/0/posts"
                              parameters:params
                                 success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         [[KSPostsController sharedController] addPosts:responseObject];
     }
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         if (block) {
             block([NSArray array], error);
         }
     }];

This way I don't stack a ton of logic in the success and failure blocks.

Upvotes: 1

Related Questions