0

After some successful HTTP request I want to return totalArray in a callback, but only after all the request has been successful. After doing some research, I found that I should somehow use something like grand central dispatch or NSLock, but I'm not sure how to use them. How can I do this?

+ (void)httpRequestOnParameters:(NSURL*)url parametersArray:(NSArray*)parametersArray success:(Success)success failure:(Failure)failure {

    NSMutableArray* totalArray = [NSMutableArray new];
    for (NSDictionary* parameter in parametersArray) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:[url absoluteString]
          parameters:parameter
             success:^(AFHTTPRequestOperation *operation, id responseObject) {

                 [totalArray addObject:responseObject];
             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             }];
    }

    success(totalArray);
}

1 Answer 1

1

You could try using a counter set to 0 before the first async call. Then in the Success code block, increment the counter and place an if block checking for when the counter reaches your desired number of async calls.

counter = 0;
numAsyncCalls = x;
array = [ ];

asyncCall {
   mainCode: ...
   success: counter ++;
                  array.push(callResult);
                  if (counter == numAsyncCalls){
                     ''Execute desired code on
                     ''populated array[]
                  }
 }

Does that make sense? Sorry if this post looks janky, I'm trying out the stack exchange app on my iPad.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.