你好,我是RxJava的新手,我有一个接收Flowable<Item> f2的类,我需要从它获取值,而不需要更改任何数据(将值保存到本地缓存)。然后将其与其他Flowable f1连接,并将其发送到更高级别的类。是否可以从f2中只发出一次值
另外,我如何对来自Flowable f1的所有项目执行操作,但在n个项目之后从f1创建新的Flowable f2。
发布于 2019-05-28 14:43:04
对于您的第一个问题,doOnNext()可能就是您正在寻找的(http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#doOnNext-io.reactivex.functions.Consumer-)。
private static void main() {
Flowable<String> f2 = Flowable.just("a", "b", "c", "d", "e");
Flowable<String> f1 = Flowable.just("z", "x", "y");
f2.doOnNext(n -> System.out.println("saving " + n))
.concatWith(f1)
.subscribe(System.out::println);
Flowable.timer(10, SECONDS) // Just to block the main thread for a while
.blockingSubscribe();
}对于你的第二个问题,这取决于你是否想摆脱第n个问题之后的项目。如果是,您可以使用take(),如果不是,您可以查看buffer()。
public static void main(String[] args) {
Flowable<String> f1 = Flowable.just("a", "b", "c", "d", "e");
Flowable<String> f2 = Flowable.just("z", "x", "y");
f1.doOnNext(n -> System.out.println("action on " + n))
.take(3)
.subscribe(System.out::println);
System.out.println("------------------------");
System.out.println("Other possible use case:");
System.out.println("------------------------");
f1.doOnNext(n -> System.out.println("another action on " + n))
.buffer(3)
.flatMap(l -> Flowable.fromIterable(l).map(s -> "Hello " + s))
.subscribe(System.out::println);
Flowable.timer(10, SECONDS) // Just to block the main thread for a while
.blockingSubscribe();
}您可以查看Flowable (http://reactivex.io/RxJava/2.x/javadoc/index.html?io/reactivex/Flowable.html)的RxJava javadoc。它有很多运算符,大理石图很好地解释了每个运算符的作用。
https://stackoverflow.com/questions/56330797
复制相似问题