ios - 使用reverseGeocodeLocation:设置地址字符串,然后从方法返回

标签 ios objective-c cocoa-touch clgeocoder completionhandler

我尝试将起点和终点本地化为地址字符串,以便将其存储到NSUserDefaults中。问题在于该方法继续执行,并且未设置我的变量。

NSLog(@"Begin");

__block NSString *returnAddress = @"";

[self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
    if(error){
        NSLog(@"%@", [error localizedDescription]);
    }

    CLPlacemark *placemark = [placemarks lastObject];

    startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                          placemark.subThoroughfare, placemark.thoroughfare,
                          placemark.postalCode, placemark.locality,
                          placemark.administrativeArea,
                          placemark.country];
    returnAddress = startAddressString;

    //[self.view setUserInteractionEnabled:YES];
}];
NSLog(returnAddress);

NSLog(@"Einde");

这是我的应用程序调试器显示的内容:

开始
爱因德

例如,如果我的所在地地址为:“Mainstreet 32​​,CITY”。然后,我想看的是以下内容:

开始
城市Mainstreet 32
爱因德

问题是我的代码没有等待CLGeocoder完成,因此返回时未设置我的变量returnAddress,并且它为空。

有谁知道如何解决这个问题?

最佳答案

因为reverseGeocodeLocation有一个完成块,所以当执行到达时,它会移交给另一个线程-但在主线程上的执行仍将继续进行下一个操作,即NSLog(returnAddress)。此时,尚未设置returnAddress,因为reverseGeocodeLocation刚刚移交给了另一个线程。

在使用完成块时,您必须开始考虑异步工作。

考虑将reverseGeocodeLocation保留为方法中的最后一个操作,然后使用完成块中的其余逻辑来调用新方法。这将确保在您拥有returnAddress的值之前,逻辑不会执行。

- (void)someMethodYouCall 
{
    NSLog(@"Begin");
    __block NSString *returnAddress = @"";

    [self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
        if(error){
            NSLog(@"%@", [error localizedDescription]);
        }

        CLPlacemark *placemark = [placemarks lastObject];

        startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                              placemark.subThoroughfare, placemark.thoroughfare,
                              placemark.postalCode, placemark.locality,
                              placemark.administrativeArea,
                              placemark.country];
        returnAddress = startAddressString;

        //[self.view setUserInteractionEnabled:YES];

        NSLog(returnAddress);
        NSLog(@"Einde");

        // call a method to execute the rest of the logic 
        [self remainderOfMethodHereUsingReturnAddress:returnAddress];
    }];
// make sure you don't perform any operations after reverseGeocodeLocation.
// this will ensure that nothing else will be executed in this thread, and that the
// sequence of operations now follows through the completion block.
}

- (void)remainderOfMethodHereUsingReturnAddress:(NSString*)returnAddress {
   // do things with returnAddress.
}

或者,您可以在reverseGeocodeLocation完成后使用NSNotificationCenter发送通知。您可以在其他需要的地方订阅这些通知,并从那里完成逻辑。将[self remainderOfMethodHereWithReturnAddress:returnAddress];替换为:
NSDictionary *infoToBeSentInNotification = [NSDictionary dictionaryWithObject:returnAddress forKey:@"returnAddress"];

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"NameOfNotificationHere" 
    object:self
    userInfo: infoToBeSentInNotification];
    }];

Here's使用NSNotificationCenter的示例。

关于ios - 使用reverseGeocodeLocation:设置地址字符串,然后从方法返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14346516/

相关文章:

后台/ sleep 模式下的 iOS Coremotion 加速器更新

ios - 如何模块化监听通知的类?

iphone - 由于 iPhone 应用程序中的 "message sent to deallocated instance"导致崩溃不一致

objective-c - NSNotificationCenter 获取 NSMutableDictionary addObject 上的通知

android - 如何使 MonoTouch 跨平台

android - 您可以将 Flutter 项目导出到 Java 和 Swift 吗?

ios - 检查应用程序的今日小部件是否已安装

ios - Voip 服务 (PKPush) 委托(delegate)方法未被调用

objective-c - 为 BeaaS 创建一个包装器(Parse/Stackmob/...)

objective-c - 使用背景颜色在 UIDatePicker 上设置圆角半径