配列を列挙して NSURL を作成し、JSON を返す NSURLSessionDataTask を呼び出すメソッドを呼び出しています。ループは通常約 10 回実行されますが、日によって異なる場合があります。
データの処理を開始する前に、for ループとすべての NSURLSessionDataTasks が完了するまで待つ必要があります。
すべての作業がいつ完了するかを把握するのに苦労しています。メソッド全体がいつ完了するかを知るための方法やロジックを誰かが推奨できますか (ループおよびデータタスクの場合)?
-(void)findStationsByRoute{
for (NSString *stopID in self.allRoutes) {
NSString *urlString =[NSString stringWithFormat:@"http://truetime.csta.com/developer/api/v1/stopsbyroute?route=%@", stopID];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse.statusCode == 200){
NSError *jsonError = [[NSError alloc]init];
NSDictionary *stopLocationDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
NSArray *stopDirectionArray = [stopLocationDictionary objectForKey:@"direction"];
for (NSDictionary * _stopDictionary in stopDirectionArray) {
NSArray *stop = [_stopDictionary objectForKey:@"stop"];
[self.arrayOfStops addObject:stop];
}
}
}];
[task resume];
}
}