Swift:使用异步方法获取值

Sco*_*ott 1 asynchronous reverse-geocoding ios clgeocoder swift

我正在为这种情况寻找一两个好的习语:

我想使用异步反向地理定位调用将 CLLocationCooperative2D 转换为 CLPlacemark,作为一系列其他操作的一部分。

转换步骤在很大程度上是一个“实用”步骤,因此在处理程序中放入大量代码来执行“其他操作”感觉结构很差。

我可以将结果存储在类变量中,但我需要知道异步步骤何时完成,这意味着某种事件触发或主线程排队超时或其他什么,这也看起来很尴尬。

有标准方法吗?将代码放入处理程序中是否很常见?

谢谢!

这是我的上下文的具体代码,FWIW。

func getPlaceFromCoordinate(coordinate: CLLocationCoordinate2D) -> CLPlacemark? {

    var loc = CLLocation(
        latitude: coordinate.latitude,
        longitude: coordinate.longitude
    )

    var mightBeAPlace: CLPlacemark? = nil

    CLGeocoder().reverseGeocodeLocation(loc, completionHandler: {(placemarks, error) -> Void in
        if(error != nil) {
            println("Reverse geocoding error.")
        }
        else if (placemarks.count == 0) {
            println("no placemarks")
        }
        else { // if (placemarks.count > 0)
            println("we have placemarks")
            mightBeAPlace = CLPlacemark(placemark: placemarks[0] as! CLPlacemark)
            println("Inside closure place: \(mightBeAPlace?.locality)")
            lastUserSelectedPlace = mightBeAPlace // This stores it in a class variable.
        }
    })
    println("Outside closure place: \(mightBeAPlace?.locality)")
    return mightBeAPlace // This of course fails because the async task is running separately.
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 6

典型的方法是自己采用该completionHandler方法,例如:

lazy var geocoder = CLGeocoder()

func getPlaceFromCoordinate(coordinate: CLLocationCoordinate2D, completionHandler: (CLPlacemark!, NSError?) -> ()) {
    let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

    geocoder.reverseGeocodeLocation(location) { placemarks, error in
        if error != nil {
            println("Reverse geocoding error: \(error)")
        } else if placemarks.count == 0 {
            println("no placemarks")
        }

        completionHandler(placemarks.first as? CLPlacemark, error)
    }
}
Run Code Online (Sandbox Code Playgroud)

你会这样称呼它:

getPlaceFromCoordinate(coordinate) { placemark, error in 
    if placemark != nil {
        // use placemark here
    }
}

// but do not use it here, because the above runs asynchronously (i.e. later)
Run Code Online (Sandbox Code Playgroud)

completionHandler就在这个闭包中放入多少代码以及在 中放入多少代码而言getPlaceFromCoordinate,这完全取决于该代码所包含的内容。但是,在 中重复的例行代码(例如记录错误,你有什么)getPlaceFromCoordinate,并且希望闭包将仅限于获取CLPlacemark和更新模型对象和/或 UI。

但是,是的,约定是将任何取决于异步方法完成情况的内容放入完成处理程序中。虽然有一些技术可以使这种异步方法同步运行,但这通常是一个非常糟糕的主意。

如果您发现闭包内的代码变得难以处理,请进行功能分解并将此代码移至其自己的函数中,然后让完成处理程序简单地调用它。或者还有其他异步模式(例如,NSOperation它们之间具有依赖关系的异步子类、promise/futures 等)。但使用异步模式。