I don't understand why the code is not executing serially. I have called the closures syncronously so that the next closure will be called only after the completion of previous one. On terminal, the last closure is printed first which I was expecting to execute in the end, then the second one & then the first one at last. Any help on this ?
func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,
let location = placemarks.first?.location?.coordinate
else {
completion(nil)
return
}
completion(location)
}
}
let queue = DispatchQueue(label: "queue")
queue.sync {
self.getLocation(from: self.sourceString) { location in
if(location != nil){
self.source = MKPlacemark(coordinate: location!)
print("Source found")
}
else{
print("Source not found")
}
}
}
queue.sync {
self.getLocation(from: self.sourceString) { location in
if(location != nil){
self.destination = MKPlacemark(coordinate: location!)
print("Destination found")
}
else{
print("Destination not found")
}
}
}
queue.sync {
if(self.source.coordinate.latitude != 0.0 && self.destination.coordinate.latitude != 0.0 ){
self.bringMap = true
}
else{
print("coordinates not updated yet")
}
}