0
如何管理在循環中調用異步塊?循環中的異步塊
我的代碼被稱爲n次:
- (void) convertToCountries:(NSString*)time longitude:(NSString*)lon latitude:(NSString*)lat {
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]];
[geocoder reverseGeocodeLocation:myLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
if(placemarks && placemarks.count > 0)
{
//do something
CLPlacemark *topResult = [placemarks objectAtIndex:0];
NSString *addressTxt = [NSString stringWithFormat:@"%@, %@ %@,%@ %@", [topResult country],
[topResult subThoroughfare],[topResult thoroughfare],
[topResult locality], [topResult administrativeArea]];
NSLog(@"%@",addressTxt);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
[DataOperations saveRecord:[topResult country] time:time];
}
}];
}
我需要從這些電話收集輸出數據。
任何幫助將不勝感激。
你有什麼問題? – sergio
由於它是異步調用,它不會等待處理結果,然後繼續。循環只用第一個參數完成。 – Vanya
你會怎麼做?對呼叫進行排序(例如,等待一個完成,並使用結果)? – sergio