中间完成处理程序不等待异步方法完成

时间:2016-05-24 22:16:05

标签: ios swift asynchronous mapkit

我使用中间处理程序等待reverseGeocodeLocation获取响应,以便我可以更新全局变量以供将来的视图控制器使用。即使在包含reverseGeocodeLocation的函数中使用中间完成处理程序之后,主队列仍然在方法调用之前。

func getReversedGeocodeLocation(completionHandler: (String, NSError?) ->()){
    CLGeocoder().reverseGeocodeLocation(newAddressLocation, completionHandler: {(placemarks, error) -> Void in


        if error != nil {
            print("Reverse geocoder failed with error" + error!.localizedDescription)
            return
        }
        else if placemarks?.count > 0 {
            print("reverseGeocode success")
        }
        else {
            print("Problem with the data received from geocoder")
        }

        completionHandler(placemarks!.first!.name! ?? "", error)

    })
}

呼叫getReversedGeocodeLocation

getReversedGeocodeLocation() { address, error in
            if address != "" {
                self.newAddress = address //newAddress is the variable 
                                          //I need to update
            }

        }

0 个答案:

没有答案