GMSGeoCoder reverseGeocodeCoordinate:completeHandler:在后台线程上 [英] GMSGeoCoder reverseGeocodeCoordinate: completionHandler: on background thread

查看:100
本文介绍了GMSGeoCoder reverseGeocodeCoordinate:completeHandler:在后台线程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从2个坐标中获取城市名称(我正在使用GMSGeoCoder -reverseGeocodeCoordinate: completionHandler:方法),然后比较这些对象.

I need to get the city name from 2 coordinates (I'm using GMSGeoCoder -reverseGeocodeCoordinate: completionHandler: method) and then to comapre the objects.

问题是该方法在后台线程(不在主线程中)上运行,并且当我尝试比较(使用if语句)对象(userCitystoreCity-都为NSString )仍为零.

The problem is that the method is running on a background thread (not in the main thread) and when I try to compare (using if statement) the objects (userCity and storeCity- both NSString) is still 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;
        }

有什么主意吗?谢谢!

推荐答案

在完成异步任务后重新组织代码以继续:

Restructure your code to proceed after the async tasks are done:

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

This also has the benefit that you don't actively wait for stuff and block the main thread

- (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]);
        }];
    }];
}   

这篇关于GMSGeoCoder reverseGeocodeCoordinate:completeHandler:在后台线程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆