0

I have in ViewController code

var location = CLLocation()
    DispatchQueue.global().sync {
        let objLocationManager = clsLocationManager()
        location = objLocationManager.findLocationByAddress(address: self.txtTo.stringValue)
    }
    lblToLatitude.stringValue = String(location.coordinate.latitude)
    lblToLongitude.stringValue = String(location.coordinate.longitude)

calling findLocationByAddress method which is implemented in separate class clsLocationManager like this

func findLocationByAddress(address: String) -> CLLocation {
    let geoCoder = CLGeocoder()
    var location = CLLocation()
    geoCoder.geocodeAddressString(address, completionHandler: {(places, error) in
        guard error == nil else { return }
        location = places![0].location!
    })
    return location
}

I try to ensure via DispatchQueue.global().sync that geo-coding is executed before passing coordinates to lblToLatitude and lblToLongitude labels but it doesn't work. Of course I could do geo-coding in ViewController code but I'm wondering how to keep it in separate class.

Dawy
  • 770
  • 6
  • 23
  • did you get it done i cant make it work i know im l8 but help will be much appreachiated – Bear Dec 07 '21 at 14:47

1 Answers1

0

You need a completion

func findLocationByAddress(address: String,completion:@escaping((CLLocation?) -> ())) {
    let geoCoder = CLGeocoder() 
    geoCoder.geocodeAddressString(address, completionHandler: {(places, error) in
        guard error == nil else { completion(nil) ; return }
        completion(places![0].location!)
    }) 
}

to call

findLocationByAddress { (location) in 
  if let location = location { 
      lblToLatitude.stringValue = String(location.coordinate.latitude)
      lblToLongitude.stringValue = String(location.coordinate.longitude)
  } 
}

Also no need for DispatchQueue.global().sync { as the geocoder runs asynchonously in a background thread

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Excellent answer, but we should also mention, that calling `findLocationByAddress` will cancel _any_ previous geocoder forward request (in this app) which is still pending. – CouchDeveloper Mar 03 '19 at 11:00