Swift 异步函数滞后

标签 swift multithreading asynchronous mapkit

我在尝试调用 iOS MapKit 中的 .calculate 函数时遇到问题。我知道这个函数在与主线程不同的单独线程上运行,但我不知道如何在将控制权返回到主线程之前更新 walkingTime 的值。

class RouteManager {

    static let shared = RouteManager()
    var initialWalkingTime: Double!
    var finalWalkingTime: Double!
    private init() {

    }

    func calculateWalkingTime(
                from givenLocation: CLLocation,
                to givenStop: CLLocation,
                completion: @escaping (_ double: Double?, _ error: Error?) -> () ) {

        let sourcePlacemark = MKPlacemark(coordinate: givenLocation.coordinate)
        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
        let destinationPlacemark = MKPlacemark(coordinate: givenStop.coordinate)
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
        var walkingTime: Double?

        let request = MKDirectionsRequest()
        request.source = sourceMapItem
        request.destination = destinationMapItem
        request.transportType = .walking
        request.requestsAlternateRoutes = false
        let directions = MKDirections(request: request)

        directions.calculate { response, error in
            if let route = response?.routes.first {
                walkingTime = (route.expectedTravelTime/60)
            }
            completion(walkingTime, error)
        }
    }

    func setupRoutes(from initialLocation: CLLocation, to finalLocation: CLLocation) {

        let startingLocation = BusManager.shared.startingLocation.location
        let endingLocation = BusManager.shared.endingLocation.location

        calculateWalkingTime(from: initialLocation, to: startingLocation) {(walkingTime, error) in
            guard let walkingTime = walkingTime, error == nil else {return}
            self.initialWalkingTime = walkingTime
        }

        calculateWalkingTime(from: endingLocation, to: finalLocation) {(walkingTime, error) in
            guard let walkingTime = walkingTime, error == nil else {return}
            self.finalWalkingTime = walkingTime
        }
    }
}

基本上,我的 RouteManager.shared.initialWalkingTimeRouteManager.shared.finalWalkingTime 用于我的 UI,因此我需要在它们离开我的 calculateWalkingTime 之前对其进行更新 函数。

我该如何去做呢?

最佳答案

假设计算足够短,嵌套它们可能就可以了:

func setupRoutes(from initialLocation: CLLocation, to finalLocation: CLLocation) {

    let startingLocation = BusManager.shared.startingLocation.location
    let endingLocation = BusManager.shared.endingLocation.location

    calculateWalkingTime(from: initialLocation, to: startingLocation) {(firstWalkingTime, error) in
        guard let firstWalkingTime = firstWalkingTime, error == nil else {return}

        calculateWalkingTime(from: endingLocation, to: finalLocation) {(secondWalkingTime, error) in
            guard let secondWalkingTime = secondWalkingTime, error == nil else {return}
            self.initialWalkingTime = firstWalkingTime
            self.finalWalkingTime = secondWalkingTime
        }
    }
}

请注意,在您的设计中,您可能应该将 self.initialWalkingTimeself.finalWalkingTime 设为可选,因为这是一个可能失败的过程。

关于Swift 异步函数滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719949/

相关文章:

ios - 2 使用 UIPageViewController 的页面 View

ios - 如何使 UIView 位于 UITabBarController 中所有选项卡的顶部

c++ - 线程常量错误 C++

python - 测试扭曲的应用程序 - 加载客户端

c# - 小查询应该异步吗?

ios - 将 UITableViewCell 中的 UIButton 点击​​手势绑定(bind)到 viewModel 中的可观察对象

swift - 获取当前用户发布的消息列表

Python线程越来越多地生成任务并消耗过多的内存和cpu

c# - 使用 C# 鼠标事件从服务器向客户端发送标志

javascript - Node.js - 使用异步库 - async.parallel 传递数组