如何正确地等到功能迅速完成?

时间:2019-03-18 19:59:20

标签: swift closures completionhandler

我现在已经尝试了很多东西,但是似乎都没有用。

我有一个for循环,该循环分析一些数据并将坐标转换为ZIP字符串:

for i in 0 ... results.count - 1
{
    result = results[i]
    self.coordinateToString(lat: result.lat, long: result.long, completion: { (place) in
                        someCell.label.text = place
                    })
}

func coordinateToString(lat: Double, long: Double, completion: @escaping (String) -> ()) {
    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: lat, longitude: long)
    var ret = ""

    geoCoder.reverseGeocodeLocation(location, completionHandler:
    {
        placemarks, error -> Void in

        guard let placeMark = placemarks?.first else { return }

        if let zip = placeMark.postalCode, let town = placeMark.subAdministrativeArea
        {
            let toAppend = "\(zip)" + " \(town)"
            ret = toAppend

        }
    })

    DispatchQueue.main.async {
        completion(ret)
    }
}

但是,我从来没有设法在单元格中显示正确的位置,它总是显示空白,因为它某种程度上不等待完成处理程序完成转换。我在这里做什么错了?

1 个答案:

答案 0 :(得分:2)

发生这种情况是因为reverseGeocodeLocation立即返回并且其完成处理程序随后运行。这意味着ret值在放入主队列时可能为空。您应该从回调内部将其分派到main,如下所示:

func coordinateToString(lat: Double, long: Double, completion: @escaping (String) -> ()) {
  let geoCoder = CLGeocoder()
  let location = CLLocation(latitude: lat, longitude: long)
  var ret = ""

  geoCoder.reverseGeocodeLocation(location, completionHandler:
  {
    placemarks, error -> Void in

    guard let placeMark = placemarks?.first else { return }

    if let zip = placeMark.postalCode, let town = placeMark.subAdministrativeArea
    {
        let toAppend = "\(zip)" + " \(town)"
        ret = toAppend
        DispatchQueue.main.async {
          completion(ret)
       }
    }
})

当然,在这种情况下,您需要相应地处理错误情况。更好的是,使用defer,这样无论发生什么情况都将调用完成:

func coordinateToString(lat: Double, long: Double, completion: @escaping (String) -> ()) {
  let geoCoder = CLGeocoder()
  let location = CLLocation(latitude: lat, longitude: long)
  var ret = ""

  geoCoder.reverseGeocodeLocation(location, completionHandler:
  {
    defer {
       DispatchQueue.main.async {
          completion(ret)
       }
    } 

    placemarks, error -> Void in

    guard let placeMark = placemarks?.first else { return }

    if let zip = placeMark.postalCode, let town = placeMark.subAdministrativeArea
    {
        let toAppend = "\(zip)" + " \(town)"
        ret = toAppend
    }
})