【问题标题】:CompletionHandler is not executedCompletionHandler 未执行
【发布时间】:2016-02-09 13:07:40
【问题描述】:

我尝试使用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)!
}

【问题讨论】:

    标签: swift cllocation completionhandler reversegeocodelocation


    【解决方案1】:

    这称为异步编程。当反向地理编码完成后,将调用完成处理程序。您需要在处理程序中调用 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()
               }
            }
        })
    }
    

    【讨论】:

    • 谢谢。我按照您的建议添加了一个加载器,但情况没有改变。我解决了删除变量“currentPlace”并将其替换为我要填写的标签的问题。
    • 传递的数据过多。还是谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2016-04-02
    相关资源
    最近更新 更多