Ios 并发进程执行的同步

Ios 并发进程执行的同步,ios,multithreading,Ios,Multithreading,我有一些数据处理的并发操作。在处理过程中,我需要检索位置的反向地理编码。众所周知,-(void)reverseGeocodeLocation:(CLLocation*)location completionHandler:(CLGeocodeCompletionHandler)completionHandler也在后台线程中执行地理编码请求,并在调用后立即返回。当geocoded完成请求时,它在主线程上执行完成处理程序。在地理编码器检索结果之前,如何阻止并发操作 __block CLPlacem

我有一些数据处理的并发操作。在处理过程中,我需要检索位置的反向地理编码。众所周知,
-(void)reverseGeocodeLocation:(CLLocation*)location completionHandler:(CLGeocodeCompletionHandler)completionHandler也在后台线程中执行地理编码请求,并在调用后立即返回。当geocoded完成请求时,它在主线程上执行完成处理程序。在地理编码器检索结果之前,如何阻止并发操作

__block CLPlacemark *_placemark

- (NSDictionary *)performDataProcessingInCustomThread
{
    NSMutableDictionary *dict = [NSMutableDictionary alloc] initWithCapacity:1];
    // some operations

    CLLocation *location = [[CLLocation alloc] initWithLatitude:40.7 longitude:-74.0];
    [self proceedReverseGeocoding:location];

    // wait until the geocoder request completes

    if (_placemark) {
        [dict setValue:_placemark.addressDictionary forKey:@"AddressDictionary"];

    return dict;
}

- (void)proceedReverseGeocoding:(CLLocation *)location
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([error code] == noErr) {
            _placemark = placemarks.lastObject;
        }
    }];
}

为了实现这一点,我们可以使用
dispatch\u semaphore\u t
。首先,我们需要向类中添加信号量:

dispatch_semaphore_t semaphore;
当我们处理数据并准备接收地理编码数据时,我们应该创建调度信号量并开始等待信号:

semaphore = dispatch_semaphore_create(0);
[self proceedReverseGeocoding:location];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
在CLGeocodeCompletionHandler结束时,我们只需发送信号以恢复数据处理:

dispatch_semaphore_signal(semaphore);
在此之后,数据处理将继续