Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 循环中的异步块_Objective C_Ios_Asynchronous_Cllocation - Fatal编程技术网

Objective c 循环中的异步块

Objective c 循环中的异步块,objective-c,ios,asynchronous,cllocation,Objective C,Ios,Asynchronous,Cllocation,如何管理在循环中调用异步块 我的代码将被调用n次: - (void) convertToCountries:(NSString*)time longitude:(NSString*)lon latitude:(NSString*)lat { CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]]; [geocoder rever

如何管理在循环中调用异步块

我的代码将被调用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];

                   }
               }];

}
我需要收集这些调用的输出数据


任何帮助都将不胜感激。

给您的对象一个属性,
BOOL dunloadin

然后,在完成块中,将
self.dunloadin
设置为
YES

最后,您的循环应该如下所示:

for (int i=0; i<10; i++)
{
    self.dunloadin = NO;
    [self convertToCountries:…];

    while (!self.dunloadin)
    {
        [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
}

for(int i=0;i您遇到了什么问题?因为它是异步调用,所以不会等到处理结果后再继续。循环只使用第一个参数完成。您会做什么?对调用排序(例如,等待一个完成,然后使用它的结果)?我在询问关于这种情况的最佳方法。我有n条带纬度和经度的记录,需要使用此函数调用这些参数以获得每条记录的结果。我递归地执行了此操作,但我不喜欢此解决方案。