0

我是 ReactiveSwift 的初学者。我创建了天气应用程序,但我的请求不起作用。

func fetchCurrentWeather() -> SignalProducer<TodayWeatherData?, DownloadError> {
    guard let unwrappedURL = url else { return SignalProducer.empty }

    return URLSession.shared.reactive
        .data(with: URLRequest(url: unwrappedURL))
        .retry(upTo: 2)
        .flatMapError { error in
            print("Error = \(error.localizedDescription)")
            return SignalProducer.empty
        }
        .map { (data, response) -> TodayWeatherData? in
            do {
                let weatherArray = try JSONDecoder().decode(TodayWeatherData.self, from: data)
                return weatherArray
            } catch (let error) {
                print("\(error)")
                return nil
            }
        }
        .observe(on: UIScheduler())
}

self.weatherFetcher.fetchCurrentWeather().map { weather in 

}

不调用地图块。我应该在这个请求或解析方法中改变什么?

4

1 回答 1

1

你必须开始你的SignalProducer.

self.weatherFetcher.fetchCurrentWeather().startWithResult({ result in 
    switch result {
       case .success(let weather): //use the result value
       case .failed(let error): //handle the error
    }

})

你也有

  • startWithFailed()
  • startWithValues()
  • startWithCompleted()
  • 开始()

在所有情况下,您都必须“启动”冷信号才能使其工作。

于 2018-09-10T13:43:48.673 回答