GMSGeoCoder reverseGeocodeCoordinate: completionHandler: 在后台线程
GMSGeoCoder reverseGeocodeCoordinate: completionHandler: on background thread
我需要从 2 个坐标中获取城市名称(我正在使用 GMSGeoCoder
-reverseGeocodeCoordinate: completionHandler:
方法)然后比较对象。
问题是该方法 运行 在后台线程(不在主线程)上,当我尝试比较(使用 if
语句)对象(userCity
和 storeCity
- NSString
) 仍然是零。
我的代码:
//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]);
}];
}];
}
我需要从 2 个坐标中获取城市名称(我正在使用 GMSGeoCoder
-reverseGeocodeCoordinate: completionHandler:
方法)然后比较对象。
问题是该方法 运行 在后台线程(不在主线程)上,当我尝试比较(使用 if
语句)对象(userCity
和 storeCity
- NSString
) 仍然是零。
我的代码:
//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]);
}];
}];
}