Search code examples
androidrx-java2rx-android

RxJava2 changing type of Flowable object being returned by API


The DarkSkyAPI call returns Forecast object containing WeeklyData object that in turn contains Array<DailyData>.

My Repository class requires Array<DailyData> to cache and propagate the data to the Presenter.

Currently I am calling the API like this: Flowable<Forecast> response = service.getRxWeatherResponse(params...);.

How can I unwrap this Flowable<Forecast> to extract Flowable<Array<DailyData>> to be returned to the Repository class?

Thank you.


Solution

  • Got it Chris, thanks! I've used the map operator as you advised. Final code returns Observable and looks like this:

    return service.getRxWeatherResponse(API cal params...)
            .map(new Function<Forecast, List<DailyData>>() {
                     @Override
                     public List<DailyData> apply(Forecast forecast) throws Exception {
                         return forecast.getWeeklyData().getDailyData();
                     }
                 });
    

    Or simplified using lambda:

    return service.getRxWeatherResponse(API cal params...)
            .map(forecast -> forecast.getWeeklyData().getDailyDataArray());