Reputation: 498
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
appTokenGot
) step3 else callFailurelistGot
) step5 else callFailure
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
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
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