CompletionHandler is not executed



我试图使用 CLGeocoder.reverseGeocodeLocation 从坐标获取"局部性",但是当执行达到 completionHandler 时,括号内的代码被完全跳过以self.currentPlace = (self.place?.locality)! + ", " + (self.place?.thoroughfare)!冻结应用程序。

我错在哪里??

func updateLocation() {
    let location = CLLocation.init(latitude: currentCoordinates.latitude, longitude: currentCoordinates.longitude)
    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error)  in
        if (error != nil) {
            print("Error")
        }else {
        let pm = placemarks as [CLPlacemark]!
        if pm.count > 0 {
        self.place = pm.first
        self.stopUpdatingLocation()
           }
        }
    })
    self.currentPlace = (self.place?.locality)! + ", " + (self.place?.thoroughfare)!
}

称为异步编程。当反向地理编码完成后,将在一段时间后调用完成处理程序。您需要在处理程序中调用 UI 更新。

您可以显示某种加载程序,该加载程序向用户指示操作正在进行中。

func updateLocation() {
    let location = CLLocation.init(latitude: currentCoordinates.latitude, longitude: currentCoordinates.longitude)
    let geocoder = CLGeocoder()
    //Show loader here
    geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error)  in
        //hide loader here
        if (error != nil) {
            print("Error")
        }else {
        let pm = placemarks as [CLPlacemark]!
        if pm.count > 0 {
        self.place = pm.first
        self.currentPlace = (self.place?.locality)! + ", " + (self.place?.thoroughfare)!
        self.stopUpdatingLocation()
           }
        }
    })
}

相关内容

  • 没有找到相关文章

最新更新