RxJava 和 Retrofit - Rx 的第一步

RxJava and Retrofit - first steps with Rx

使用 RxJava(没有 Retrolambda),我想做一些 API 调用并用它完成我的数据。我的不完整对象是一个 'TvShow',其中包含一个对象列表 'Season'。这个'Season'是空的,我需要用剧集补全。

Observable<TvShow> getDataTVShow(long idTvShow)
//get TvShow with empty seasons (except season number)

Observable<Season> getDataSeason(long idTvShow, int seasonNumber); 
//get one complete season with episodes

所以我想:

到目前为止,我只有:

Observable<TvShow> = apiService.getDataTvShow(idTvShow)

我现在需要遍历季节,我尝试使用运算符 'map' 从 'TvShow' 对象切换到我的季节列表 (tvShow.getSeasons()) 但我不确定是否处于良好状态。除此之外,我知道 'doOnNext' 将用于更新我的 'old' 赛季,仅此而已。

我尝试使用这个很好的例子:Handling lists with RxJava and Retrofit in android 但我仍然卡住了 :(

如果你能帮我解决这个问题,那就太好了:)

例如你有两个观察值:

Observable<Season> getSeason(int id)
Observable<TvShow> getTvShow(String id)

然后加载每个季节并填充电视节目的电视节目有多响亮:

  Observable<TvShow> getFilledTvShow = getTvShow("123")
      .flatMap(tvShow ->
              //make stream observable from seasons
              Observable.from(tvShow.seasons)
                  //load each season from network
                  .flatMap(season -> getSeason(season.id))
                      //then collect all results to ArrayList
                  .collect(() -> new ArrayList<Season>(),
                      (seasons, filledSeason) -> seasons.add(filledSeason))
                      //finally fill tvShow and return it
                  .map(filledSeasons_ -> {
                     tvShow.seasons = filledSeasons_;
                     return tvShow;
                  })
      );