RxJava2 Flowable dematerialize && materialize(条辅助操作符)
dematerialize && materialize
目录
dematerialize
1 dematerialize接口
<T2> Flowable<T2> |
dematerialize()
Returns a Flowable that reverses the effect of 返回一个Flowable,它通过将Source Publisher发出的Notification对象转换为它们所代表的项目或通知来反转实现的效果。 |
2 dematerialize图解
这里使用了materialize的图解,
3 dematerialize测试用例
@Test
public void dematerialize() {
System.out.println("######dematerialize#####");
Notification onNext = Notification.createOnNext("onNext");
Notification onComplete = Notification.createOnComplete();
Flowable.just(onNext, onNext, onNext, onComplete).dematerialize().subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
System.out.println("accept:" + o.toString());
}
});
}
结果输出
######dematerialize#####
accept:onNext
accept:onNext
accept:onNext
4 dematerialize测试用例说明
dematerialize是与materialize相反过来的操作,materialize将在后面讲,将Notification对象转换成数据并发射出去,Notification包括三种类型的通知,onNext、onError,onComplete。
上面的测试用例发射了三个onNext通知,一个onComplete通知,由于onComplete通知就是事件的结束,所以没有打印。
materialize
1 materialize接口
Flowable<Notification<T>> |
materialize()
Returns a Flowable that represents all of the emissions and notifications from the source Publisher into emissions marked with their original types within |
2 materialize图解
materialize操作符将数据转换为一个Notification的对象,这个对象有下图三个类型
3 materialize测试用例
@Test
public void materialize() {
System.out.println("######materialize#####");
Flowable.just("item1", "item2").materialize().subscribe(new Consumer<Notification<String>>() {
@Override
public void accept(Notification<String> stringNotification) throws Exception {
System.out.println("stringNotification:" + stringNotification.toString());
}
});
}
测试输出
######materialize#####
stringNotification:OnNextNotification[item1]
stringNotification:OnNextNotification[item2]
stringNotification:OnCompleteNotification
Process finished with exit code 0
4 materialize测试用例说明
materialize刚好与dematerialize相反,将数据转换成了Notification,测试输出stringNotification:OnNextNotification[item1],item1就是转换前的数据,所有动作结束时会调用OnComplete通知,所以最后的输出是:stringNotification:OnCompleteNotification