I have a function that takes a [CLLocation]
as an input. Inside a while
loop it splits it into chunks, for each chunk makes a MKDirection
request, store the response in a new [CLLocation]
and returns it once completed.
The problem is that all the chunks in the new array are not sequential, so the resulting route jumps all over the place. Ho do I wait for the previous request to get a response before making a new one? I tried DispatchQueue.global().sync
and DispatchQueue.main.sync
but it doesn't make a difference.
I tried to implement first answer from Cannot wait for the result of MKDirections.calculate, getting nil instead of it that seemed my same problem, but I'm not understanding how to adapt it to my case.
Can you please help me to get the responses sequentially?
This is the function, the commented out part is for the lates bit of the route, and that will be the the last request.
As always many thanks for your help and time.
func repositionLocation2(route: [CLLocation], completion: @escaping ([CLLocation]) -> Void) {
let group = DispatchGroup()
var pos = 0
var nextPos = 3
var repositioned = [CLLocation]()
// repositioned.append(route.first!)
guard route.count > nextPos else {print("Reposision Location failed, not enough positions");return}
let request = MKDirections.Request()
request.requestsAlternateRoutes = false
request.transportType = .walking
while pos < route.count - nextPos {
print(" pos in \(pos)")
// repositioned.removeAll()
group.enter()
// get a small chunk of the input route
let a = route[pos].coordinate//repositioned.last!.coordinate//
let b = route[pos + nextPos].coordinate
// get directions for the small chunk
request.source = MKMapItem(placemark: MKPlacemark(coordinate: a))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: b))
let directions = MKDirections(request: request)
// DispatchQueue.main.sync {
// DispatchQueue.global().sync {
// group.enter()
directions.calculate { [unowned self] response, error in
if let err = error {
print("direction error : \(err)")
}
guard let unwrappedResponse = response else {print("no suggested routes available"); return }
print("Response is: \(unwrappedResponse.debugDescription)")
guard let coord = unwrappedResponse.routes.first?.steps else {print("No coordinates");return}
print("coord is: \(coord)")
// save response coordinates into a new array
for location in coord {
let point: CLLocation = CLLocation(latitude: location.polyline.coordinate.latitude, longitude: location.polyline.coordinate.longitude)
print("point is: \(point)") // prints a correct CLLocation with coordinates
repositioned.append(point)
print("repositioned in for loop is : \(repositioned)") // prints just first appended location CLLocation with coordinates
// group.leave()
}
// group.wait() // hangs the app
completion(repositioned)
}
// }
print("repositioned in while loop is : \(repositioned)")
// shift to nex addiacent chunk
pos += 3
nextPos += 3
}
// // last chunk
// let a = route[pos - 5].coordinate//repositioned.last!.coordinate
// let b = route.last?.coordinate
// request.source = MKMapItem(placemark: MKPlacemark(coordinate: a))
// request.destination = MKMapItem(placemark: MKPlacemark(coordinate: b!))
// let directions = MKDirections(request: request)
// directions.calculate { [unowned self] response, error in
// if let err = error {
// print("direction error : \(err)")
// }
// guard let unwrappedResponse = response else {print("no suggested routes available"); return }
// print("Response is: \(unwrappedResponse.debugDescription)")
// guard let coord = unwrappedResponse.routes.first?.steps else {print("No coordinates");return}
// print("coord is: \(coord)")
// for location in coord {
//
// let point: CLLocation = CLLocation(latitude: location.polyline.coordinate.latitude, longitude: location.polyline.coordinate.longitude)
// print("point is: \(point)")
// repositioned.append(point)
// print("repositioned in for loop is : \(repositioned)")
// }
// completion(repositioned)
// }
// print("repositioned in while loop is : \(repositioned)")
}