尝试调用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.initialWalkingTime
和RouteManager.shared.finalWalkingTime
用于我的用户界面,因此我需要在离开calculateWalkingTime
功能之前对其进行更新。
我该怎么做?
答案 0 :(得分:0)
假设计算足够短,那么嵌套它们可能很好:
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.initialWalkingTime
和self.finalWalkingTime
,因为这是一个无法完成的过程。