MKDirections computeETAWithCompletionHandler延迟执行Swift 2.0 [英] MKDirections calculateETAWithCompletionHandler executing with delay Swift 2.0

查看:94
本文介绍了MKDirections computeETAWithCompletionHandler延迟执行Swift 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MKDirections类中的computeETAWithCompletionHandler在Swift中计算两个坐标之间的ETA.我的程序中包含以下代码,

I'm trying to calculate the ETA between two coordinates in Swift using calculateETAWithCompletionHandler from the MKDirections class. I have the following code in my program,

MapViewController.swift

print("A")
calculateETAofClosestBus(closestBusStop)
print("B")

func calculateETAofClosestBus(destination: BusStop) {
    var shortestETA = 0

    print("C")
    var request = MKDirectionsRequest()
    var sourceItem = MKMapItem(placemark: MKPlacemark(coordinate: listBuses[0].pin.coordinate, addressDictionary: nil))
    request.source = sourceItem
    request.transportType = .Automobile
    let destinationCoordinates = CLLocationCoordinate2D(latitude: destination.coordinate.latitude, longitude: destination.coordinate.longitude)
    let destinationItem = MKMapItem(placemark: MKPlacemark(coordinate: destinationCoordinates, addressDictionary: nil))
    request.destination = destinationItem
    request.requestsAlternateRoutes = false
    var directions = MKDirections(request: request)
    print("D")
    directions.calculateETAWithCompletionHandler { (etaResponse, error) -> Void in
        print("E")
        if let error = error {
            print("Error while requesting ETA : \(error.localizedDescription)")
        } else {
            print("F")
            shortestETA = Int((etaResponse?.expectedTravelTime)!)
        }
    }
    print("G")
}

我包括了打印语句,以显示代码的执行情况.当我运行程序时,得到的输出是

I included print statements to show the execution of the code. When I run the program, the output I get is

A
C
D
G
B
E
F

因此,我注意到,calculateETAofClosestBus()函数完成执行(到达G,然后到达B),但是随后执行 directions.calculateETAWithCompletionHandler ,因此打印了'E'和'F'<从"calculateETAofClosestBus()"返回的strong> AFTER .

So, I've noticed that the calculateETAofClosestBus() function finishes executing (reaches G, then B), but then directions.calculateETAWithCompletionHandler executes, so 'E' and 'F' get printed AFTER we've returned from calculateETAofClosestBus().

我确定我不明白calculateETAWithCompletionHandler的工作原理,但是我想通过汽车来计算两个注解之间的ETA,以及是否有更直观的方法来完成此操作,或者是否有人可以帮助我理解Handler更好,将不胜感激.

I'm sure I don't understand how calculateETAWithCompletionHandler works, but I would like to calculate the ETA between two Annotations via an automobile and if there is a more intuitive way to accomplish this or if someone could help me understand the Handler better, that would be greatly appreciated.

解决方案:

iOS-Swift-返回异步获取值的函数帮助我了解如何做到最好.

iOS - Swift - Function that returns asynchronously retrieved value helped me understand how to accomplish this best.

推荐答案

请查看以下内容

Please look ath this Link it clearly states calculateETAWithCompletionHandler works asynchronously .Since asynchronous, it works on different thread.But your main thread is executing on its own way and prints G

开始计算所需的旅行时间信息 异步.

Begins calculating the requested travel-time information asynchronously.

如果要返回异步方法,则可以使用闭包.这是

If you would like to return in the asynchronous method..You can do using closure.Here is the link

这篇关于MKDirections computeETAWithCompletionHandler延迟执行Swift 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆