0

I am beginner in ReactiveSwift. I create weather app and my request does not work.

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 

}

Map block is not called. What should i change in this request or in parsing method ?

1 Answers1

1

You have to start your SignalProducer.

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

})

you also have

  • startWithFailed()
  • startWithValues()
  • startWithCompleted()
  • start()

in all cases, you have to "start" cold signals in order to make them work.

Alchi
  • 799
  • 9
  • 19