0

我知道它与锁或调度组有关,但我似乎无法对其进行编码......

在离开该方法之前,我需要知道该地址是否为有效地址。当前线程刚刚溢出并返回 TRUE。我试过锁,调度员工作,但似乎无法正确。任何帮助表示赞赏:

- (BOOL) checkAddressIsReal
{
    __block BOOL result = TRUE;

    // Lets Build the address
    NSString *location = [NSString stringWithFormat:@" %@ %@, %@, %@, %@", streetNumberText.text, streetNameText.text, townNameText.text, cityNameText.text, countryNameText.text];

    // Put a pin on it if it is valid

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error) {
        result = [placemarks count] != 0;
    }];

    return result;
}
4

2 回答 2

0

文档说在主线程上CLGeocoder调用。completionHandler由于您可能还从主线程调用您的方法,因此它不能等待地理编码器的回答而不给它提供结果的机会。

这将通过轮询运行循环来完成,使用一些 API 作为-[NSRunLoop runMode:beforeDate:].

缺点是根据模式,这也会在等待结果时传递事件和触发计时器。

于 2012-11-06T13:37:10.370 回答
0

只需使用块作为参数:

- (void) checkAddressIsRealWithComplectionHandler:(void (^)(BOOL result))complectionHandler
{
    __block BOOL result = TRUE;

    // Lets Build the address
    NSString *location = [NSString stringWithFormat:@" %@ %@, %@, %@, %@", streetNumberText.text, streetNameText.text, townNameText.text, cityNameText.text, countryNameText.text];

    // Put a pin on it if it is valid

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error) {
                     result = [placemarks count] != 0;
                     complectionHandler(result);
                 }];
}
于 2015-06-05T11:06:04.480 回答