将 Observable 与第二个 Observable 结合使用,后者使用第一个 Observable 的结果 [英] Combine Observable with second Observable which uses results from from the first one
本文介绍了将 Observable 与第二个 Observable 结合使用,后者使用第一个 Observable 的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两种返回 Observable 的方法:
I have two methods returning Observable:
Observable<String> firstObservable();
Observable<String> secondObservable(String value);
对于第一个 Observable 的每个结果,我都会得到第二个 Observable 的新实例.对于第二个 observable 的每个结果,我将返回带有组合结果的对象.
For each result from the first Observable I get new instance of the second Observable. For each result from the second observable I would return object with combined results.
firstObservable -> x----x----x----x----x
\ \ \ \ \
secondObservable -> y(x)-y(x)-y(x)-y(x)-y(x)
\ \ \ \ \
result -> {x,y}-{x,y}-{x,y}-{x,y}-{x,y}
如何做到这一点?
推荐答案
flatMap
有一个重载变体,它的第二个参数是组合函数,它可以访问初始项和由第二个 observable 产生:
There's an overloaded variant of flatMap
, the second argument of which is the combining function that has access to the initial item and the one produced by the second observable:
firstObservable.flatMap(string -> secondObservable(string), (s, s2) -> s + s2);
这篇关于将 Observable 与第二个 Observable 结合使用,后者使用第一个 Observable 的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文