我将在我的应用程序中显示一种 UIActivityIndicatorView,同时在 for () 循环中解析多个 JSON 对象。我想不出我必须在哪里放置 [UIActivityIndicatorView startAnimating] 和 [UIActivityIndicatorView stopAnimating] 以显示解析的真正结束(在完整的 for () 循环的末尾)。这是简化的代码:
- (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
}
如您所见,我找不到放置 [UIActivityIndicatorView stopAnimating] 的真正位置,因为它在第一个解析结束时停止(第一个 for () 循环):所以,有一种方法可以等待完成for () 循环在整个循环之后调用 [UIActivityIndicatorView stopAnimating] (或其他方法)?谢谢!
替代方案 也许解析太快以至于我看不到 UIActivityIndicatorView 出现和加载?在这种情况下,我必须在哪里放置一种计时器(或睡眠(无符号整数))等待几秒钟才能消失?