如何在创建另一个请求之前等待多重嵌套请求?

How to wait for a multiply nested requests before creating another requests?

我有一个刷新控件。当我把它拉下来时,我会发出 2 个这样的嵌套请求:

  httpClient.fetchCurrentWeather(...
    httpclient.fetchAnotherPartOfWeather(...
      delegate.showWeather(...)
    )
   )

我想等待第一组完成后再调用另一组,因为当我这样做时,由于许多回调值更新,我的视图会闪烁。

更新: 我在交互器中调用:

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)
        }
    }

在这里您不需要太高级的东西。你只需要 main a Boolean(例如:isRequestIncomplete)来检查是否收到了上一个请求的响应。以下是如何实现此目的的示例:

if isRequestIncomplete { return }
isRequestIncomplete = true
httpClient.fetchCurrentWeather(...
    httpclient.fetchAnotherPartOfWeather(...
      isRequestIncomplete = false
      delegate.showWeather(...)
    )
   )