使用reverseGeocodeLocation:设置地址字符串并从方法中返回 [英] Set address string with reverseGeocodeLocation: and return from method

查看:265
本文介绍了使用reverseGeocodeLocation:设置地址字符串并从方法中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将开始和结束点本地化到地址字符串,以便我可以将其存储到 NSUserDefaults 。问题是该方法继续执行,并且不设置我的变量。

I'm try to localize a start and end point to an address string, so that I can store it into NSUserDefaults. The problem is that the method continues executing and does not set my variable.

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");

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

This is what my application debugger shows:

开始

einde

start
einde

如果我的位置地址:Mainstreet 32​​,CITY。然后我会看到以下内容:

If for example the address of my location is : "Mainstreet 32, CITY". Then what i would want to see is the following:


开始

Mainstreet 32​​,CITY

Einde

Start
Mainstreet 32, CITY
Einde

问题是我的代码不等待我的 CLGeocoder 来完成,所以我的变量 returnAddress 在返回时不会设置,并且为空。

The problem is that my code does not wait for my CLGeocoder to finish, so my variable returnAddress is not set when it is returned, and it is empty.

因为 reverseGeocodeLocation 有一个可以解决这个问题的方法,所有人都知道如何解决这个问题。

Does anyone know how to work around this?

推荐答案

完成块,当执行到达时,它被切换到另一个线程 - 但是在主线程上的执行将继续到下一个操作,这是 NSLog(returnAddress)。此时, returnAddress 尚未设置,因为 reverseGeocodeLocation 刚刚切换到另一个线程。

Because reverseGeocodeLocation has a completion block, it is handed off to another thread when execution reaches it - but execution on the main thread will still continue onto the next operation, which is NSLog(returnAddress). At this point, returnAddress hasn't been set yet because reverseGeocodeLocation was JUST handed off to the other thread.

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

When working with completion blocks, you'll have to start thinking about working asynchronously.

code> reverseGeocodeLocation 作为方法中的最后一个操作,然后使用完成块中的逻辑的剩余部分调用新方法。这将确保逻辑不会执行,直到您具有 returnAddress 的值。

Consider leaving reverseGeocodeLocation as the last operation in your method, and then calling a new method with the remainder of the logic inside the completion block. This will ensure that the logic doesn't execute until you have a value for 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.
}

也可以使用NSNotificationCenter发送通知 reverseGeocodeLocation 完成。您可以在任何需要的地方订阅这些通知,并从那里完成逻辑。替换 [self remainderOfMethodHereWithReturnAddress:returnAddress]; 与:

Or you can use NSNotificationCenter to send a notification when reverseGeocodeLocation is complete. You can subscribe to these notifications anywhere else you need it, and complete the logic from there. Replace [self remainderOfMethodHereWithReturnAddress:returnAddress]; with:

NSDictionary *infoToBeSentInNotification = [NSDictionary dictionaryWithObject:returnAddress forKey:@"returnAddress"];

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

这里是使用NSNotificationCenter的示例。

Here's an example of using NSNotificationCenter.

这篇关于使用reverseGeocodeLocation:设置地址字符串并从方法中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆