1
votes

Trying to get name of a city, while having latitude and longitude. Inside a model class Location, I'm using reverseGeocodeLocation(location: , completionHandler: ) func that comes with CLGeocoder (part of CoreLocation).

func getLocationName() {

    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: currentLatitude, longitude: currentLongitude)

    geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
        guard let addressDict = placemarks?[0].addressDictionary else {
            return
        }

        if let city = addressDict["City"] as? String {
            self.currentCity = city
            print(city)
        }
        if let zip = addressDict["ZIP"] as? String {
            print(zip)
        }
        if let country = addressDict["Country"] as? String {
            print(country)
        }
    })
}

However, in ViewController, after running the getLocationName(), the location.currentCity is nil, since the completion handler is async, and wasn't finished yet.

How can I make sure that the completion handler is finished running so I can access location.currentCity ?

2
Just access location.currentCity in the completion handler or add another completion handler to getLocationName() to return the data asynchronously. - vadian
You don't "make sure that the completion handler is finished running", you just do whatever you want to do with location.currentCity in the completionHandler - Andrew Brooke
@vadian i need to access 'location.currentCity' in view controller, (outside of Location class) to updated the UI. Will it be better to add another completion handler so i'm not updating UI within a model class? Can you provide an example code how that new completion handler should be implemented? - Mr_Vlasov
@AndrewBrooke but I want to update my UI with location.currentCity in viewController and I don't have instances of my view controller within model class 'Location'. Should I create that instance, so i can do 'vcInstance.updateUI(currentCity)' somewhere within completion handler? - Mr_Vlasov

2 Answers

3
votes

Pass a closure as a function parameter in your getLocationName which you can call inside the reverseGeocodeLocation closure.

func updateLocation(currentCity : String) -> Void
{
  print(currentCity)
}

func getLocationName(callback : @escaping (String) -> Void)
{

  let geoCoder = CLGeocoder()
  let location = CLLocation(latitude: currentLatitude, longitude: currentLongitude)

  geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
     guard let addressDict = placemarks?[0].addressDictionary else {
        return
     }


     if let city = addressDict["City"] as? String
     {
         self.currentCity = city
         callback(city)

         print(city)
      }
      if let zip = addressDict["ZIP"] as? String {
         print(zip)
      }
      if let country = addressDict["Country"] as? String {
        print(country)
      }
   })
 }

In your ViewController...

getLocationName(callback: updateLocation)
0
votes

I would create a function where location.currentCity is used, and call this function from the completion handler

So if your code looks like:

func foo() {
    var location
    getLocationName()
    print(location.currentcity) // nil
}

change it to:

func foo() {
    var location
    getLocationName()
}

func bar() {
    print(location.currentcity) // someplace
}

and call bar() from your completion handler