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 [UIActivityIndicatorView startAnimating] and [UIActivityIndicatorView stopAnimating] to show the real END of parsing (at the end of the complete for () loop). This is the simplified code:
- (void)parseMethod {
// other stuff
for (int i=0; i < [arrayJSON count]; i++) {
NSURL *url = [NSURL URLWithString:
[NSString stringWithFormat:
@"http://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@",[arrayJSON objectAtIndex:i]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSMutableDictionary *arrayMain = [JSON objectForKey:@"main"];
NSMutableDictionary *arrayMain2 = [JSON objectForKey:@"main2"];
[arrayA1 addObject:[arrayMain valueForKey:@"A1"]];
[arrayA2 addObject:[arrayWind valueForKey:@"A2"]];
// HERE FINISH PARSING AFJSONREQUESTOPERATION
// IF I PUT HERE [UIActivityIndicatorView stopAnimating] IT SHOWS AT THE END OF FIRST for () LOOP
[table reloadData];
[table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", [error userInfo]);
}];
[operation start];
// HERE START PARSING AFJSONREQUESTOPERATION
[UIActivityIndicatorView startAnimating]
}
// HERE FINISH THE for () LOOP ??
// IF I PUT HERE [UIActivityIndicatorView stopAnimating] IT SHOWS AGAIN AT THE END OF FIRST for () LOOP
}
// HERE FINISH THE parseMethod ??
// IF I PUT HERE [UIActivityIndicatorView stopAnimating] IT SHOWS AGAIN AT THE END OF FIRST for () LOOP
}
As u can see, I can’t find the true place to put [UIActivityIndicatorView stopAnimating], ‘cos everywhere it stop at the end of the FIRST parsing (first for () loop): so, there is a way to WAIT the complete for () loop to call [UIActivityIndicatorView stopAnimating] (or another method) AFTER the ENTIRE cycle? Thanks!
ALTERNATIVE
Maybe the parsing is so fast that I can’t see the UIActivityIndicatorView appearing and loading? In this case, WHERE I must put a sort of timer (or the sleep(unsigned int)) to wait a few seconds before it disappear?
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.