Created
January 8, 2024 00:36
-
-
Save bstillitano/15dfa4af1d76974289a595bea44e27f9 to your computer and use it in GitHub Desktop.
Async/Await GMSPlace Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GooglePlaces | |
func getAddresses(withQuery query: String) async -> [GMSPlace]? { | |
let client = GMSPlacesClient.shared() | |
let token = GMSAutocompleteSessionToken.init() | |
let filter = GMSAutocompleteFilter() | |
let predictions: [GMSAutocompletePrediction]? = await withCheckedContinuation { continuation in | |
client.findAutocompletePredictions( | |
fromQuery: query, filter: filter, sessionToken: token, | |
callback: { (results, error) in | |
guard error == nil else { | |
continuation.resume(returning: nil) | |
return | |
} | |
guard let results = results else { | |
continuation.resume(returning: nil) | |
return | |
} | |
continuation.resume(returning: results) | |
}) | |
} | |
guard | |
let places: [GMSPlace?] = await predictions?.asyncMap({ prediction in | |
return await withCheckedContinuation { continuation in | |
client.lookUpPlaceID(prediction.placeID) { place, error in | |
continuation.resume(returning: place) | |
} | |
} | |
}) | |
else { return nil } | |
return places.compactMap({ $0 }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment