RxJava 嵌套 Observables 暴露值
RxJava Nested Observables exposing values
我有以下代码,可以按预期工作。但是,我不喜欢 blockingGet。有没有办法将 getSingle() 调用向上移动到 Observable 的 'global' 变量中以避免这种情况?换句话说,我希望 getSingle 在继续代码之前不使用 blockingGet 来完成。请注意,在地图调用中,我需要访问 foos、bar 和 getSingle() 响应。
如有任何帮助,我们将不胜感激!
public List<Baz> example(Single<Collection<Foo>> foosSingle, Collection<Bar> bars) {
return foosSingle
.flatMap(foos ->
Observable.fromIterable(bars)
.map(bar -> getBaz(foos, getSingle(bar.getBlah()).blockingGet(), bar.getFlaz())
.toList()
);
}
FWIW 我试着用这样的东西替换 .map 调用:
.map(bar -> Lists.ArrayList(bar, getSingle(bar.getBlah()).blockingGet())
.map(list -> getBaz(foos, list.get(1), list.get(0).getFlaz())
这让我可以访问我需要的所有变量,但它没有解决 blockingGet 问题。
使用.flatMap
代替地图
public List<Baz> example(Single<Collection<Foo>> foosSingle, final Collection<Bar> bars) {
return foosSingle
.flatMap(foos ->
Observable.fromIterable(bars)
.flatMapSingle(bar -> {
return getSingle(bar.getBlah())
.map(x -> getBaz(foos, x, bar.getFlaz()))
})
.toList()
);
}
我有以下代码,可以按预期工作。但是,我不喜欢 blockingGet。有没有办法将 getSingle() 调用向上移动到 Observable 的 'global' 变量中以避免这种情况?换句话说,我希望 getSingle 在继续代码之前不使用 blockingGet 来完成。请注意,在地图调用中,我需要访问 foos、bar 和 getSingle() 响应。
如有任何帮助,我们将不胜感激!
public List<Baz> example(Single<Collection<Foo>> foosSingle, Collection<Bar> bars) {
return foosSingle
.flatMap(foos ->
Observable.fromIterable(bars)
.map(bar -> getBaz(foos, getSingle(bar.getBlah()).blockingGet(), bar.getFlaz())
.toList()
);
}
FWIW 我试着用这样的东西替换 .map 调用:
.map(bar -> Lists.ArrayList(bar, getSingle(bar.getBlah()).blockingGet())
.map(list -> getBaz(foos, list.get(1), list.get(0).getFlaz())
这让我可以访问我需要的所有变量,但它没有解决 blockingGet 问题。
使用.flatMap
代替地图
public List<Baz> example(Single<Collection<Foo>> foosSingle, final Collection<Bar> bars) {
return foosSingle
.flatMap(foos ->
Observable.fromIterable(bars)
.flatMapSingle(bar -> {
return getSingle(bar.getBlah())
.map(x -> getBaz(foos, x, bar.getFlaz()))
})
.toList()
);
}