问题描述
使用RxJava(不Retrolambda),我愿做一些API调用,完成我的数据吧。我的不完整的对象是一个TvShow'与对象'季节的列表。这个季节是空的,我需要用情节来完成它。
With RxJava (without Retrolambda), I would like to do some API calls and complete my data with it. My incomplete object is a 'TvShow' with a list of object 'Season'. This 'Season' is empty and I need to complete it with episodes.
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
所以我想:
- 获取我的'TvShow'对象的(OK)
- 迭代赛季(名单&LT; \\赛季>)从我的'TvShow对象和每个季节做一个API调用,让我的赛季完全结束,然后在列表更新我的'老'的季节。
- 然后,一旦我们有我们所需要的,持久化数据到数据库中(用户部分)
到现在为止,我只有:
Observable<TvShow> = apiService.getDataTvShow(idTvShow)
我现在需要遍历赛季,我试图用运算符'地图',从'TvShow'对象切换到我的季节名单(tvShow.getSeasons()),但我不知道是在好办法。除了这一点,我知道'doOnNext'将被用于更新我的'老'的季节,仅此而已。
I need now to iterate over seasons, I tried to use operator 'map' to switch from 'TvShow' object to my list of seasons (tvShow.getSeasons()) but I'm not sure to be in the good way. Beside that, I know 'doOnNext' will be used to update my 'old' season, that's it.
我试着用这个很好的例子的工作:Handling与RxJava并列出改造android中,但我还是坚持:(
I tried to work with this good example: Handling lists with RxJava and Retrofit in android but I'm still stuck :(
如果你能帮助我解决这个问题,这将是巨大的:)
If you can help me solve this problem, it would be great :)
推荐答案
例如,你有两个观测值:
For example you have two observables:
Observable<Season> getSeason(int id)
Observable<TvShow> getTvShow(String id)
如何负载TvShow然后加载每个赛季,并填写TvShow:
How load TvShow then load each Season and fill TvShow:
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;
})
);
请注意。我不知道,这是一个最好的解决办法,因为我不是很用RX还没有经历;
Note. I am not sure that it's a best solution, because I'm not very experienced with rx yet;
这篇关于RxJava和改造 - 与接收的第一个步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!