gpt4 book ai didi

ios - GMSGeoCoder 反向地理编码坐标 : completionHandler: on background thread

转载 作者:行者123 更新时间:2023-11-29 01:14:02 24 4
gpt4 key购买 nike

我需要从 2 个坐标获取城市名称(我正在使用 GMSGeoCoder -reverseGeocodeCooperative:completionHandler: 方法),然后对对象进行 comapre。

问题是该方法在后台线程上运行(而不是在主线程中),当我尝试比较(使用 if 语句)对象时(userCitystoreCity - NSString) 仍然是 nil。

我的代码:

//Checking user's city
__block NSString *userCity;
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:self.locationManager.location.coordinate completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error) {
NSLog(@"%@",[error description]);
}
userCity=[[[response results] firstObject] locality];
}];
//Checking store's city
__block NSString *storeCity;
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:arounder.radiusCircularRegion.center completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error) {
NSLog(@"%@",[error description]);
}
arounderCity=[[[response results] firstObject] locality];
}];
if ([userCity isEqualToString:arounderCity]) {
return YES;
}

有什么想法吗?谢谢!

最佳答案

重构您的代码以在异步任务完成后继续:

这还有一个好处,就是您不会主动等待并阻塞主线程

例如:

- (void)checkCitiesWithCompletionBlock:(void (^)(BOOL same))
//Checking user's city
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:self.locationManager.location.coordinate completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error) {
NSLog(@"%@",[error description]);
}
id userCity=[[[response results] firstObject] locality];

//Checking store's city
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:arounder.radiusCircularRegion.center completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error) {
NSLog(@"%@",[error description]);
}
id arounderCity=[[[response results] firstObject] locality];

same ([userCity isEqualToString:arounderCity]);
}];
}];
}

关于ios - GMSGeoCoder 反向地理编码坐标 : completionHandler: on background thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com