GMSGeoCoder reverseGeocodeCoordinate:completionHandler:在后台线程上
我需要从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]); }]; }]; }
- 如何在UIImageView的左上angular和右下angular创buildUIButton
- 什么是iOS应用程序的启动顺序?
- 如何使用Alamofire上传MultipartFormData进行身份validation
- Objective C为什么需要更改语言来重新启动应用程序?
- 从原始PCM 16000采样率stream中使用CMSampleTimingInfo,CMSampleBuffer和AudioBufferList
- 在tableview(iOS)中从parse.com显示数组
- 从外部类访问私有variables – iOS
- 如何使用Alamofire 4以字节为单位获得下载进度?
- Swift Objective-C运行时类的命名