GMSGeoCoder reverseGeocodeCoordinate:completionHandler:在后台线程上

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

问题是该方法在后台线程上运行(不在主线程中),当我尝试比较(使用if语句)时,对象( userCitystoreCity – 两个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]); }]; }]; }