我需要连锁两个可观察到的,第二个取决于第一个。所以我要说的是
可观察的1 ->请愿书返回可观察的
//kotlin
fun getTvShow(): Observable<TvShow> {
return retrofitPetitionGetShow()...
}
//java
Observable<TvShow> getTvShow(){
return retrofitPetitionGetShow()...
}可观测的2 ->返回Single
Observable.range(1, TvShow.totalSeasons)
.flatMap { seasonNumber: Int ->
retrofitPetitionGetSeason(seasonNumber)....
}.toList() 我需要的是添加到TvShow对象TvShow.setList(List<Season>)中的第二个可观察的结果(列表),然后返回它。
提前谢谢你
发布于 2020-07-05 08:17:29
根据注释中提供的信息,您可以尝试使用以下代码(这是Java代码,但应该很容易将其转换为Kotlin):
private Observable<TvShow> getTvShow() {
return retrofitPetitionGetShow();
}
private Single<List<Season>> getSeasons(TvShow tvShow) {
return Observable.range(1, tvShow.getTotalSeasons())
.flatMap(seasonNumber -> retrofitPetitionGetSeason(seasonNumber))
.toList();
}
public Observable<TvShow> chainObservables() {
return getTvShow()
.flatMap(tvShow -> getSeasons(tvShow).map(tvShow::withSeasons).toObservable());
}重要!
以反应性/功能的方式,您不应该修改对象,而应该创建新的对象(在您的示例中,有一个带有季节列表的tvShow更新)。有一个以这种方式实现的tvShow::withSeasons方法引用:
public TvShow withSeasons(List<Season> seasons) {
return this.seasons == seasons ? this : new TvShow(this.name, this.totalSeasons, seasons);
}https://stackoverflow.com/questions/62733730
复制相似问题