I\'m going to show in my app a sort of UIActivityIndicatorView while parsing several JSON objects, inside a for () loop. I can\'t figure WHERE I must place the [UIActivityIndica
Solution is to
Simplify the code so that is obvious what is being done there (declaring the blocks beforehand)
Count how many requests are running and perform the global actions only when all the requests are completed.
typedef void (^AFJSONSuccessBlock) (NSURLRequest *request, NSHTTPURLResponse *response, id JSON);
typedef void (^AFJSONFailureBlock) (NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON);
[UIActivityIndicatorView startAnimating];
__block int numRequests = [arrayJSON count];
AFJSONSuccessBlock onSuccess = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[...]
numRequests--;
if (numRequests == 0) {
[UIActivityIndicatorView stopAnimating];
}
};
AFJSONFailureBlock onFailure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[...]
numRequests--;
if (numRequests == 0) {
[UIActivityIndicatorView stopAnimating];
}
};
for (NSString* jsonPath in arrayJSON) {
NSString* absolutePath = [NSString stringWithFormat:@"http://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@", jsonPath];
NSURL *url = [NSURL URLWithString:absolutePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:onSuccess
failure:onFailure];
[operation start];
}