Synchronous reverseGeocodeLocation

I'm using Xcode 14.3.1

I’m working on a macOS app that needs reverse geolocation. I have the following code:

            let thisLocation = CLLocation(latitude: lat, longitude: long)
            let geocoder = CLGeocoder()
            geocoder.reverseGeocodeLocation(thisLocation, completionHandler:
            {
                placeMarks, error in

                myLocality = (placeMarks?.first?.locality ?? "") + ", " +               (placeMarks?.first?.administrativeArea ?? "")
            })

The geocoder.reverseGeocodeLocation call gets flagged with: Consider using asynchronous alternative function

First off, it is async. myLocality doesn’t get filled in until long after the call has been issued. The use of a completitionHandler guarantees that.

I tried an alternative geocoder.reverseGeocodeLocation call that really claims to be async, https://developer.apple.com/documentation/corelocation/clgeocoder/1423621-reversegeocodelocation. (This same page claims the call above is synchronous but with a completion handler. To me that’s an oxymoron, a completion handle by definition is not synchronous, https://developer.apple.com/documentation/swift/calling-objective-c-apis-asynchronously)

Replacement code:

            let thisLocation = CLLocation(latitude: lat, longitude: long)
            let geocoder = CLGeocoder()
            let locationSemaphore = DispatchSemaphore(value: 0)
            Task
            {
                do
                {
                    let placemark = try await geocoder.reverseGeocodeLocation(thisLocation)
                    myLocality = (placemark.first?.locality ?? "") + ", " + (placemark.first?.administrativeArea ?? "")
                }
                catch
                {
                    print(“Unable to Find Address for Location " + String(lat) + "," + String(long))
                }
                locationSemaphore.signal()
            } // Task
            locationSemaphore.wait()

In this second case, the semaphore.wait() call gets flagged the message: Instance method 'wait' is unavailable from asynchronous contexts; Await a Task handle instead; this is an error in Swift 6

Speaking from testing, the wait simply never gets resolved.

I will add that my reverseGeocodeLocation call is nested in another async call. I guarantee that this is my max depth of 2. My semaphore logic works elsewhere in the code creating the first async call.

My main question is how can I get the reverseGeocodeLocation synchronously, before moving on? How can I get a Task handle to wait on?

Thanks all.

Synchronous reverseGeocodeLocation
 
 
Q