0

I have a situation where I need to make multiple request to a server where subsequent request will depend on previous request

1) request 1
2) process data
3) request 2 based on data in step 2
4) process data

what's the best way to approach this for AFNetworking 2

Charlie Wu
  • 7,657
  • 5
  • 33
  • 40
  • Take a look here: http://stackoverflow.com/questions/11413156/do-nsoperations-and-their-completionblocks-run-concurrently – Lefteris Nov 08 '13 at 00:53
  • Based on this: http://stackoverflow.com/questions/11417924/how-to-perform-a-batch-of-afnetworking-requests-that-depend-on-each-other – Lefteris Nov 08 '13 at 00:54
  • problem with http://stackoverflow.com/questions/11417924/how-to-perform-a-batch-of-afnetworking-requests-that-depend-on-each-other is completion block is not called in sequence, step 2 and step 4 is running out of order – Charlie Wu Nov 08 '13 at 06:19
  • http://stackoverflow.com/questions/11413156/do-nsoperations-and-their-completionblocks-run-concurrently is interesting, the way I'm approaching this is to wrap AFNetowrking in a concurrent NSOperation and call finish in completion block, but i'm hopping there is a more elegant solution – Charlie Wu Nov 08 '13 at 06:20
  • http://stackoverflow.com/questions/25540749/run-request-operations-in-loop-inside-operation/25540928#25540928 This question could help. – SleepNot Aug 29 '14 at 01:04

2 Answers2

1

Call the second request in the completion handler of the first request. Here is some example code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON Response 1: %@", responseObject);

    // Process data here, and use it to set parameters or change the url for request2 below

    [manager GET:@"http://example.com/request2.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@"JSON Response 2: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Error 2: %@", error);
    }];
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error 1: %@", error);
}];
Dave Kasper
  • 1,369
  • 1
  • 11
  • 18
  • i thought of that, and it was the original implementation, but the issue is there is not just 2 chained request, there are 8, some of the requests depend on previous calls, i would have to chain 7 completion block to call another operation, I'm kinda hopping there is a more elegant solution. It's either that or wrap AFNetworking inside a concurrent NSOperation – Charlie Wu Nov 08 '13 at 06:22
  • Ah, very different question. You might not need to resort to NSOperation, would it work for you to wrap the GET call in a function and pass your data as one of the arguments? – Dave Kasper Nov 08 '13 at 06:26
  • David, what do you mean? you mean like calling the next request in the completion block? – Charlie Wu Nov 08 '13 at 06:42
0

I had a play around and the ended up implement my own completion block and failure block so they can be executed on the some thread as the request operation by adding a category to the AFHTTPRequestOperation class

- (void)startAndWaitWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    [self start];
    [super waitUntilFinished];

    id responseObject = self.responseObject; // need this line for AFNetworking to create error;

    if (self.error) {
        if (failure) failure(self, self.error);
    } else {
        if (success) success(self, responseObject);
    }
}

The operation will start then block the thread till the operation is completed. Then depend on success or failure, call the corresponding block before finish the operation.

This way i can chain multiple request operation one after another and the completion block will have the processed data from the previous request completion block

Charlie Wu
  • 7,657
  • 5
  • 33
  • 40