Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe the issue is that I was not using locking to increment the counters so the while loop would never evaluate to <code>true</code>.</p> <p>I was able to get it working by only looking for a fail count greater than <code>0</code> that way as long as it was incremented by any of the request callback blocks then I know what to do.</p> <p>I just so happen to have switched to <code>[NSOperationQueue waitUntilAllOperationsAreFinished]</code>.</p> <p>Final code:</p> <pre><code>/** Checks if we can communicate with the APIs @result YES if the network is available and all of the registered APIs are responsive */ - (BOOL)apisAvailable { // Check network reachability if (!_connectionAvailable) { return NO; } // Check API server response NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; __block NSInteger failedRequests = 0; for (AFHTTPClient *httpClient in _httpClients) { // Send heart beat request NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"" parameters:nil]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // Server returned good response } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Server returned bad response failedRequests += 1; }]; [operationQueue addOperation:operation]; } // Wait for heart beat requests to finish [operationQueue waitUntilAllOperationsAreFinished]; // Check final results if (failedRequests &gt; 0) { return NO; } return YES; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload