'How to use async await until variable value changes by CLlocation?
I have implemented the following code to fetch data from URL
func fetchData() async{
let urlString = "https://api.aladhan.com/v1/timingsByCity?city=\(locationManager.cityName!)&country=\(locationManager.countryName!)&method=3"
guard let url = URL(string: urlString) else {return}
do{
isFetching = true
let (data, response) = try await URLSession.shared.data(from: url)
print(data)
if let resp = response as? HTTPURLResponse, resp.statusCode >= 300{
self.errorMessage = "Failed to hit endpoint with bad status code"
}
let prayer = try JSONDecoder().decode(Prayer.self, from: data)
print(prayer)
print(prayer.data.timings.Fajr)
isFetching = false
prayerDic["Fajr"] = prayer.data.timings.Fajr
prayerDic["Dhuhr"] = prayer.data.timings.Dhuhr
prayerDic["Asr"] = prayer.data.timings.Asr
prayerDic["Maghrib"] = prayer.data.timings.Maghrib
prayerDic["Isha"] = prayer.data.timings.Isha
prayerDic["Midnight"] = prayer.data.timings.Midnight
}
catch{
isFetching = false
print("Failed to reach endpoint: \(error)")
}
}
the value I have defined in another class for cityName is "Canberra" and countryName is "Australia" but during execution the location updates successfully but does not fetch from API using the new variable received from CLlocation. How can I make a function wait until the variable value is received or updated?
NavigationLink {
Test()
} label: {
Text("\(locationManager.suburbName!), \(locationManager.stateName!), \(locationManager.countryName!)")
}
.task{
locationM.requestAlwaysAuthorization()
locationManager.requestLocation()
print(locationManager.cityName)
await CVM.fetchData()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
