设置与reverseGeocodeLocation地址字符串:从方法返回(Set address s

2019-07-18 06:07发布

我尝试将起点和终点定位到地址字符串,这样我可以把它保存到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");

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

启动
结束

举例来说,如果我的位置的地址是:“主街32 CITY”。 那么我想看到的是以下内容:

开始
主街32,CITY
Einde

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

有谁知道如何解决此问题?

Answer 1:

因为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.
}

或者你可以使用NSNotificationCenter发送通知时reverseGeocodeLocation完成。 您可以订阅这些通知其他地方你需要它,并从那里完成的逻辑。 取代[self remainderOfMethodHereWithReturnAddress:returnAddress]; 有:

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

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

下面是使用NSNotificationCenter的例子。



文章来源: Set address string with reverseGeocodeLocation: and return from method