RxJava2 Flowable do (辅助操作符)

do

 

1 do系列接口的作用

do系列的接口作用是在项目发布的的生命周期中,onNext、onComplete、onError被调用的时候调用,有点类似于观察源Publisher的序列调用生命周期,当某个点被调用的时候做一些事情

 

2 do接口

Flowable<T> doAfterNext(Consumer<? super T> onAfterNext)

Calls the specified consumer with the current item after this item has been emitted to the downstream.

调用当前项目指定的Consumer,在这个项目被发布到下游之后

(优化一下翻译)

在项目被发布到下游之后,调用当前项目指定的Consumer

Flowable<T> doAfterTerminate(Action onAfterTerminate)

Registers an Action to be called when this Publisher invokes either onComplete or onError.

当此Publisher调用onComplete或onError时,注册要调用的Action。

Flowable<T> doFinally(Action onFinally)

Calls the specified action after this Flowable signals onError or onCompleted or gets canceled by the downstream.

在此Flowable发出onError或onCompleted通知后之后调用指定的操作,或者由下游取消。

(翻译说明:signal v:发信号,onError和onCompleted是通知的两种类型)

Flowable<T> doOnCancel(Action onCancel)

Calls the cancel Action if the downstream cancels the sequence.

如果下游取消序列,则调用取消操作。

Flowable<T> doOnComplete(Action onComplete)

Modifies the source Publisher so that it invokes an action when it calls onComplete.

修改源发布服务器,以便在调用onComplete时调用操作。

Flowable<T> doOnEach(Consumer<? super Notification<T>> onNotification)

Modifies the source Publisher so that it invokes an action for each item it emits.

修改源发布服务器,以便为其发出的每个项调用操作。

Flowable<T> doOnEach(Subscriber<? super T> subscriber)

Modifies the source Publisher so that it notifies a Subscriber for each item and terminal event it emits.

修改源发布服务器,以便它通知订阅服务器它发出的每个项目和终端事件。

Flowable<T> doOnError(Consumer<? super Throwable> onError)

Modifies the source Publisher so that it invokes an action if it calls onError.

修改源Publisher,以便在调用onError时调用它。

Flowable<T> doOnLifecycle(Consumer<? super Subscription> onSubscribe, LongConsumer onRequest,Action onCancel)

Calls the appropriate onXXX method (shared between all Subscribers) for the lifecycle events of the sequence (subscription, cancellation, requesting).

调用适当的onXXX方法(在所有订阅者之间共享)以获取序列的生命周期事件(订阅,取消,请求)。

Flowable<T> doOnNext(Consumer<? super T> onNext)

Modifies the source Publisher so that it invokes an action when it calls onNext.

修改源Publisher,以便它在调用onNext时调用操作。

Flowable<T> doOnRequest(LongConsumer onRequest)

Modifies the source Publisher so that it invokes the given action when it receives a request for more items.

Flowable<T> doOnSubscribe(Consumer<? super Subscription> onSubscribe)

Modifies the source Publisher so that it invokes the given action when it is subscribed from its subscribers.

修改源发布服务器,以便在从订阅服务器订阅时调用给定的操作。

Flowable<T> doOnTerminate(Action onTerminate)

Modifies the source Publisher so that it invokes an action when it calls onComplete or onError.

修改源Publisher,以便它在调用onComplete或onError时调用操作。

 

 

3 do图解说明

RxJava2 Flowable do (辅助操作符)

 

 

4 测试用例

下面测试用例列举了接口中的部分,但是已经可以很容易看出doXXX的作用了


    @Test
    public void doAfterNext() {
        System.out.println("######doAfterNext#####");
        Flowable.just("item1", "item2")
                .doOnNext(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        System.out.println("doOnNext,s=" + s);
                    }
                })
                .doAfterNext(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        System.out.println("doAfterNext,s=" + s +"\n");
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        System.out.println("#doOnComplete#");
                    }
                })
                .doOnEach(new Consumer<Notification<String>>() {
                    @Override
                    public void accept(Notification<String> stringNotification) throws Exception {
                        System.out.println("doOnEach,stringNotification" + stringNotification.toString());
                    }
                })
                .doOnTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        System.out.println("#doOnTerminate#");
                    }
                })
                .doAfterTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        System.out.println("#doAfterTerminate#");
                    }
                })
                .doOnSubscribe(new Consumer<Subscription>() {
                    @Override
                    public void accept(Subscription subscription) throws Exception {
                        System.out.println("doOnSubscribe,subscription=" + subscription.toString());
                    }
                })
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        System.out.println("#doFinally#");
                    }
                })
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        System.out.println("subscribe,s=" + s);
                    }
                });
    }

测试输出
######doAfterNext#####
doOnSubscribe,subscription=io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber@b684286
doOnNext,s=item1
doOnEach,stringNotificationOnNextNotification[item1]
subscribe,s=item1
doAfterNext,s=item1

doOnNext,s=item2
doOnEach,stringNotificationOnNextNotification[item2]
subscribe,s=item2
doAfterNext,s=item2

#doOnComplete#
doOnEach,stringNotificationOnCompleteNotification
#doOnTerminate#
#doFinally#
#doAfterTerminate#