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")
}
}
Post
Replies
Boosts
Views
Activity
Hi, I am new to swift and currently working on a project where I need to check if a given coordinate falls on a MKRoute or not. I searched a lot but unable to find a satisfied approach.
Can someone help on this ?