天天看點

RxJava2 Flowable dematerialize && materialize(條輔助操作符)dematerialize && materialize

dematerialize && materialize

目錄

dematerialize

1 dematerialize接口

2 dematerialize圖解

3 dematerialize測試用例

4 dematerialize測試用例說明

materialize

1 materialize接口

​​​​​​​2 materialize圖解

​​​​​​​3 materialize測試用例

​​​​​​​4 materialize測試用例說明

dematerialize

1 dematerialize接口

<T2> Flowable<T2>

dematerialize()

Returns a Flowable that reverses the effect of 

materialize

 by transforming the 

Notification

 objects emitted by the source Publisher into the items or notifications they represent.

傳回一個Flowable,它通過将Source Publisher發出的Notification對象轉換為它們所代表的項目或通知來反轉實作的效果。

​​​​​​​2 dematerialize圖解

這裡使用了materialize的圖解,

RxJava2 Flowable dematerialize &amp;&amp; materialize(條輔助操作符)dematerialize &amp;&amp; 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 

Notification

 objects.

​​​​​​​2 materialize圖解

materialize操作符将資料轉換為一個Notification的對象,這個對象有下圖三個類型

RxJava2 Flowable dematerialize &amp;&amp; materialize(條輔助操作符)dematerialize &amp;&amp; materialize

​​​​​​​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