Swift, How to sync reverseGeocodeLocation function (by using semaphore or dispatch group....etc)

540 Views Asked by At

In viewDidLoad, I call reverseGeocodeLocation to get geo info. but It is async API and I need a sync flow. However, I could not sync this flow. because when I inject semaphore in the flow, reverseGeocodeLocation's completion handler is not working in forced sync flow case. (I used semaphore for sync). It just stopped and completionHandler is not invoked. What should I do? Thanks to read. The code below is my sample.

let location = CLLocation(latitude: latitude, longitude: longitude)
  
  let semaphore = DispatchSemaphore(value: 0)
  
  CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
    print("completion")
    var placeMark: CLPlacemark! = placemarks?.first
    
    semaphore.signal()
  })
  semaphore.wait()

Comment it myself. It's impossible to use reverseGeocodeLocation synchronous.

The link below (Apple public) it explain.

https://developer.apple.com/documentation/corelocation/clgeocoder/1423621-reversegeocodelocation

Geocoding requests are rate-limited for each app, so making too many requests in a short period of time may cause some of the requests to fail. When the maximum rate is exceeded, the geocoder passes an error object with the value CLError.Code.network to your completion handler.

0

There are 0 best solutions below