I have two API call's for data and if openWeatherData fails it prevents planData.trainingdata from loading. Is there a way to workaround this so planData still loads?
List(planData.trainingdata) { index in
ForEach(openWeatherData.openWeatherData) { day in
...
}
}
Here's the API call for openWeather
class DownloadOpenWeatherData: ObservableObject {
@Published var openWeatherData: [OpenWeatherDecoder] = []
let locationManager = LocationManager()
var openWeatherCancellabes = Set<AnyCancellable>()
init() {
print("OpenWeather: loading init")
locationManager.startLoc()
getOpenWeatherData(weatherUrl: "https://api.openweathermap.org/data/2.5/onecall?lat=\(locationManager.getLat())&lon=\(locationManager.getLong())&exclude=hourly,minutely&appid")
locationManager.stopLoc()
}
func getOpenWeatherData(weatherUrl: String) {
guard let weatherUrl = URL(string: weatherUrl) else { return }
print("getOpenWeather url \(weatherUrl)")
URLSession.shared.dataTaskPublisher(for: weatherUrl)
.subscribe(on: DispatchQueue.global(qos: .background))
.receive(on: DispatchQueue.main)
.tryMap { (data, response) -> Data in
print(response)
guard
let response = response as? HTTPURLResponse,
response.statusCode >= 200 && response.statusCode < 300 else {
throw URLError(.badServerResponse)
}
print("data \(data)")
return data
}
.decode(type: OpenWeatherDecoder.self, decoder: JSONDecoder())
.sink { (completion) in
print(completion)
} receiveValue: { (returnedWeatherData) in
self.openWeatherData = [returnedWeatherData]
print("returnedWeatherData \(returnedWeatherData)")
}
.store(in: &openWeatherCancellabes)
}
}