0

Here's my implementation.

I have favCitiesID = Observable<[Int]> that will be flatMap and map. Each city id will be used in API call that will return Observable<CityMappable>. I have reached to the point where I can get [Observable<CityMappable>] but I want to transform it into Observable<[CityMappable]> so I can bind it to tableview datasource.

let favCitiesID: Observable<[Int]> = Observable.of([0,1,2])

let observableCities = favCitiesID.flatMap { cityIds -> Observable<[CityMappable]> in
        return cityIds.map{ return self.apiManager.getCurrentWeatherData(for: $0)}
}

This is APIManager function definition

func getCurrentWeatherData(for cityID: Int)->Observable<CityMappable>
cloudy45man
  • 391
  • 2
  • 19

1 Answers1

2

You can use combine to convert from [Observable<CityMappable>] to Observable<[CityMappable]>.

Try this code

let observableCities = favCitiesID.flatMap { cityIds -> Observable<[CityMappable]> in
    let obs = cityIds.map{ return self.apiManager.getCurrentWeatherData(for: $0)}
    return Observable.combineLatest(obs)
}
Thanh Vu
  • 1,599
  • 10
  • 14
  • 1
    Thank you so much!! It works. Now I'm gonna go and learn how combineLatest work. – cloudy45man Sep 01 '19 at 13:59
  • 1
    Here's an article about different ways to combine two or more Observables. https://medium.com/@danielt1263/recipes-for-combining-observables-in-rxswift-ec4f8157265f – Daniel T. Sep 01 '19 at 17:55