我有一个刷新控件。当我将其拉下时,会发出2个嵌套请求,例如:
httpClient.fetchCurrentWeather(...
httpclient.fetchAnotherPartOfWeather(...
delegate.showWeather(...)
)
)
我想等第一组完成后再调用另一组,因为当我这样做时,由于许多回调值更新,我的观点会眨眼。
upd:我在交互器中打电话:
func retreiveCurrentDailyWeatherForecast() {
guard let lat = locations?.first?.coordinate.latitude,
let lon = locations?.first?.coordinate.longitude
else { return }
httpClient.fetchCurrentWeather(
parameters: ["lat": lat,
"lon": lon, "units": "metric"],
completionHandler: { dailyWeatherResult in
switch dailyWeatherResult {
case .success(let dailyWeatherResponse):
self.httpClient.fetchCurrentHourlyWeather(
parameters: ["lat": lat,
"lon": lon, "units": "metric"],
completionHandler: { dailyWeeklyHourlyResult in
switch dailyWeeklyHourlyResult {
case .success(let dailyWeeklyHourlyResponse):
let weatherForecast =
self.modelConverter.convertWeatherForecast(dailyWeatherResponse,
dailyWeeklyHourlyResponse)
self.didRetreieveWeatherForecastFromNetwork(weatherForecast)
case .failure(let err):
print(err)
}
})
case .failure(let err):
print(err)
}
})
}
func didRetreieveWeatherForecastFromNetwork(_ weatherForecast: WeatherForecast?) {
if let weatherForecast = weatherForecast {
self.presenter.didRetreiveWeatherForecast(weatherForecast)
}
}