GMSGeocoder reverseGeocodeCoordinate 函数不适用于后台线程

问题描述

我正在尝试从后台线程调用来自 GMSGeocoder 类的 reverseGeocodeCoordinate 函数。但它不起作用。未调用完成处理程序。如果我从主线程调用函数,它工作正常。 但是这种方法有时会使应用程序变慢,因为此函数将被连续调用。 有没有办法从后台线程调用reverseGeocodeCoordinate函数

以下是我当前的代码

dispatchQueue.main.async {
                let geoCoder = GMSGeocoder()
                geoCoder.reverseGeocodeCoordinate((coordinate),completionHandler: {
                    reverseGeoCodeResponse,error in
                    // processing
                 }
}

代码有效。但是,如果我将 dispatchQueue.main 更改为 dispatchQueue.global(),则不会调用 completionHandler。我想在后台调用这个 API。

解决方法

为什么不直接从主线程调用呢?看起来调用是在后台线程上发送的,文档说回调是在主线程上调用的。

您不需要在后台线程上进行调用,也不需要确保在主线程上完成。