1、作用

  • 辅助被观察者(Observable) 在发送事件时实现一些功能性需求

     2、类型

     3、详解

          3.1   subscribe() /  subscribeOn() /  observeOn()

       作用:订阅  /  设置被观察者线程  /  设置观察者线程

 

          3.2   delay() ----- 见rxdocs.pdf第157页

       作用:使得被观察者延迟一段时间再发送事件

 

    public static void delay() {
        Observable.just(1, 2, 3)
                .delay(3, TimeUnit.SECONDS)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe");
                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "onNext: value = " + value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: " + e.toString());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");
                    }
                });
    }

        输出:

08-06 19:38:09.177 15886 15886 D Operation: onSubscribe
08-06 19:38:12.178 15886 15904 D Operation: onNext: value = 1
08-06 19:38:12.179 15886 15904 D Operation: onNext: value = 2
08-06 19:38:12.179 15886 15904 D Operation: onNext: value = 3
08-06 19:38:12.179 15886 15904 D Operation: onComplete

 

          3.3   do() ----- 见rxdocs.pdf第161页

       作用:在事件发送 & 接收的整个生命周期过程中进行操作

 

    public static void doOperation() {
        Observable.just(1, 2)
                .doOnNext(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.d(TAG, "doOnNext: value = " + integer);
                    }
                })
                .doOnLifecycle(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d(TAG, "doOnLifecycle Consumer: disposable  = " + disposable.isDisposed());
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "doOnLifecycle Action");
                    }
                })
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "doFinally");
                    }
                })
                .doAfterTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "doAfterTerminate");
                    }
                })
                .doOnTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "doOnTerminate");
                    }
                })
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d(TAG, "doOnSubscribe: disposable = " + disposable.isDisposed());
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d(TAG, "doOnError");
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "doOnComplete");
                    }
                })
                .doAfterNext(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.d(TAG, "doAfterNext: value = " + integer);
                    }
                })
                .doOnEach(new Consumer<Notification<Integer>>() {
                    @Override
                    public void accept(Notification<Integer> integerNotification) throws Exception {
                        Log.d(TAG, "doOnEach: notification = " + integerNotification.toString());
                    }
                })
                .doOnEach(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "doOnEach: onSubscribe");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "doOnEach: onNext = " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "doOnEach: onError");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "doOnEach: onComplete");
                    }
                })
                .doOnDispose(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "doOnDispose");
                    }
                })
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "real onSubscribe");
                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "real onNext: value = " + value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "real onError: " + e.toString());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "real onComplete");
                    }
                });
    }

          输出:

08-06 20:10:46.205 18692 18692 D Operation: doOnLifecycle Consumer: disposable  = false
08-06 20:10:46.205 18692 18692 D Operation: doOnSubscribe: disposable = false
08-06 20:10:46.205 18692 18692 D Operation: real onSubscribe
08-06 20:10:46.205 18692 18692 D Operation: doOnNext: value = 1
08-06 20:10:46.206 18692 18692 D Operation: doOnEach: notification = OnNextNotification[1]
08-06 20:10:46.207 18692 18692 D Operation: doOnEach: onNext = 1
08-06 20:10:46.207 18692 18692 D Operation: real onNext: value = 1
08-06 20:10:46.207 18692 18692 D Operation: doAfterNext: value = 1
08-06 20:10:46.207 18692 18692 D Operation: doOnNext: value = 2
08-06 20:10:46.207 18692 18692 D Operation: doOnEach: notification = OnNextNotification[2]
08-06 20:10:46.207 18692 18692 D Operation: doOnEach: onNext = 2
08-06 20:10:46.207 18692 18692 D Operation: real onNext: value = 2
08-06 20:10:46.207 18692 18692 D Operation: doAfterNext: value = 2
08-06 20:10:46.207 18692 18692 D Operation: doOnTerminate
08-06 20:10:46.207 18692 18692 D Operation: doOnComplete
08-06 20:10:46.207 18692 18692 D Operation: doOnEach: notification = OnCompleteNotification
08-06 20:10:46.207 18692 18692 D Operation: doOnEach: onComplete
08-06 20:10:46.208 18692 18692 D Operation: real onComplete
08-06 20:10:46.208 18692 18692 D Operation: doAfterTerminate
08-06 20:10:46.208 18692 18692 D Operation: doFinally

 

          3.4   onErrorReturn() ----- 见rxdocs.pdf第151页

       作用:遇到错误时,发送1个特殊事件 & 正常终止。捕获在它之前发生的异常

 

    public static void onErrorReturn() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new NullPointerException("null point exception"));
                emitter.onNext(3);
            }
        }).onErrorReturn(new Function<Throwable, Integer>() {
            @Override
            public Integer apply(Throwable throwable) throws Exception {
                Log.d(TAG, "onErrorReturn");
                return 666;
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          输出:

08-07 09:05:49.377 23696 23696 D Operation: onSubscribe
08-07 09:05:49.377 23696 23696 D Operation: onNext: value = 1
08-07 09:05:49.377 23696 23696 D Operation: onNext: value = 2
08-07 09:05:49.377 23696 23696 D Operation: onErrorReturn
08-07 09:05:49.377 23696 23696 D Operation: onNext: value = 666
08-07 09:05:49.378 23696 23696 D Operation: onComplete

 

          3.5   onErrorResumeNext() ----- 见rxdocs.pdf第151页

       作用:遇到错误时,发送1个新的Observable

                     注意:onErrorResumeNext()拦截的错误 = Throwable,即Error和Exception都可以拦截。

 

 

 

    public static void onErrorResumeNext() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new ClassFormatError("format error"));
            }
        }).onErrorResumeNext(new Function<Throwable, ObservableSource<? extends Integer>>() {
            @Override
            public ObservableSource<? extends Integer> apply(Throwable throwable) throws Exception {
                return Observable.just(6, 8);
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          输出:

08-09 15:14:01.506 20890 20890 D Operation: onSubscribe
08-09 15:14:01.506 20890 20890 D Operation: onNext: value = 1
08-09 15:14:01.506 20890 20890 D Operation: onNext: value = 2
08-09 15:14:01.507 20890 20890 D Operation: onNext: value = 6
08-09 15:14:01.507 20890 20890 D Operation: onNext: value = 8
08-09 15:14:01.507 20890 20890 D Operation: onComplete

 

          3.6   onExceptionResumeNext() ----- 见rxdocs.pdf第152页

     作用:遇到异常时,发送1个新的Observable

                 注意:与onErrorResumeNext()不同的是,如果onError收到的Throwable不是一个Exception,它会将错误传递给观察者的onError方法,不会使用备用的Observable。

 

    public static void onExceptionResumeNext() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new NullPointerException("null exception"));
            }
        }).onExceptionResumeNext(new Observable<Integer>() {
            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onNext(3);
                observer.onNext(4);
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

      输出:

08-09 15:29:38.250 22073 22073 D Operation: onSubscribe
08-09 15:29:38.250 22073 22073 D Operation: onNext: value = 1
08-09 15:29:38.250 22073 22073 D Operation: onNext: value = 2
08-09 15:29:38.250 22073 22073 D Operation: onNext: value = 3
08-09 15:29:38.250 22073 22073 D Operation: onNext: value = 4

 

          3.7   retry() ----- 见rxdocs.pdf第153页

     作用:重试,即当出现错误时,让被观察者(Observable)重新发射数据。Retry操作符不会将原始Observable的onError通知传递给观察者,它会订阅这个Observable,再给它一次机会无错误地完成它的数据序列。Retry总是传递onNext通知                                    给观察者,由于重新订阅,可能会造成数据项重复。

                   类型: 

 

<-- 1. retry() -->
// 作用:出现错误时,让被观察者重新发送数据
// 注:若一直错误,则一直重新发送

<-- 2. retry(long time) -->
// 作用:出现错误时,让被观察者重新发送数据(具备重试次数限制
// 参数 = 重试次数
 
<-- 3. retry(Predicate predicate) -->
// 作用:出现错误后,判断是否需要重新发送数据(若需要重新发送& 持续遇到错误,则持续重试)
// 参数 = 判断逻辑

<--  4. retry(new BiPredicate<Integer, Throwable>) -->
// 作用:出现错误后,判断是否需要重新发送数据(若需要重新发送 & 持续遇到错误,则持续重试
// 参数 =  判断逻辑(传入当前重试次数 & 异常错误信息)

<-- 5. retry(long time,Predicate predicate) -->
// 作用:出现错误后,判断是否需要重新发送数据(具备重试次数限制
// 参数 = 设置重试次数 & 判断逻辑

 

 

 

    public static void retry() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new NullPointerException("null pointer"));
                emitter.onNext(3);
            }
        }).retry(5).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          输出:

08-09 15:44:01.585 22956 22956 D Operation: onSubscribe
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 1
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 2
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 1
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 2
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 1
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 2
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 1
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 2
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 1
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 2
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 1
08-09 15:44:01.586 22956 22956 D Operation: onNext: value = 2
08-09 15:44:01.586 22956 22956 D Operation: onError: java.lang.NullPointerException: null pointer

 

          3.8   retryUntil()

     作用:出现错误后,判断是否需要重新发送数据。作用类似于retry(Predicate predicate)

    public static void retryUtil() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new NullPointerException("null pointer"));
                emitter.onNext(3);
            }
        }).retryUntil(new BooleanSupplier() {
            @Override
            public boolean getAsBoolean() throws Exception {
                return false;
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          输出:

08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 1
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 2
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 1
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 2
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 1
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 2
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 1
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 2
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 1
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 2
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 1
08-09 15:58:29.424 24651 24651 D Operation: onNext: value = 2
....

 

          3.9   retryWhen() ----- 见rxdocs.pdf第154页

     作用:遇到错误时,将发生的错误传递给一个新的被观察者(Observable),并决定是否需要重新订阅原始被观察者(Observable)& 发送事件。

                                如果这个Observable发射了一项数据,它就重新订阅,如果这个Observable发射的是onError通知,它就将这个通知传递给观察者然后终止。

    public static void retryWhen() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new NullPointerException("null pointer"));
                emitter.onNext(3);
            }
        }).retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(Observable<Throwable> throwableObservable) throws Exception {
                return throwableObservable.flatMap(new Function<Throwable, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(Throwable throwable) throws Exception {
                        //不重新发送订阅事件,发送error后终止
                        //return Observable.error(throwable);

                        //则原始的Observable重新发送事件(若持续遇到错误,则持续重试)
                        return Observable.just(6);
                    }
                });
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          输出:

以上return Observable.error(throwable)的结果:
08-09 16:14:06.579 25485 25485 D Operation: onSubscribe
08-09 16:14:06.580 25485 25485 D Operation: onNext: value = 1
08-09 16:14:06.580 25485 25485 D Operation: onNext: value = 2
08-09 16:14:06.580 25485 25485 D Operation: onError: java.lang.NullPointerException: null pointer


以上return Observable.just(6)的结果:
08-09 16:19:23.689 25958 25958 D Operation: onSubscribe
08-09 16:19:23.689 25958 25958 D Operation: onNext: value = 1
08-09 16:19:23.689 25958 25958 D Operation: onNext: value = 2
08-09 16:19:23.689 25958 25958 D Operation: onNext: value = 1
08-09 16:19:23.689 25958 25958 D Operation: onNext: value = 2
08-09 16:19:23.690 25958 25958 D Operation: onNext: value = 1
08-09 16:19:23.690 25958 25958 D Operation: onNext: value = 2
08-09 16:19:23.690 25958 25958 D Operation: onNext: value = 1
08-09 16:19:23.690 25958 25958 D Operation: onNext: value = 2
......

 

          3.10   repeat() ----- 见rxdocs.pdf第52页

        作用:无条件地、重复发送 被观察者事件

 

    public static void repeat() {
        Observable.just(6, 7)
                .repeat(3L)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe");
                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "onNext: value = " + value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: " + e.toString());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");
                    }
                });
    }

          输出:

08-09 16:33:48.097 27105 27105 D Operation: onSubscribe
08-09 16:33:48.097 27105 27105 D Operation: onNext: value = 6
08-09 16:33:48.097 27105 27105 D Operation: onNext: value = 7
08-09 16:33:48.097 27105 27105 D Operation: onNext: value = 6
08-09 16:33:48.097 27105 27105 D Operation: onNext: value = 7
08-09 16:33:48.097 27105 27105 D Operation: onNext: value = 6
08-09 16:33:48.097 27105 27105 D Operation: onNext: value = 7
08-09 16:33:48.097 27105 27105 D Operation: onComplete

 

          3.11   repeatWhen() ----- 见rxdocs.pdf第53页

        作用:有条件地、重复发送 被观察者事件。与retryWhen类似

        原理:若新被观察者(Observable)返回1个Complete / Error事件,则不重新订阅 & 发送原来的 Observable;

                                    若新被观察者(Observable)返回其余事件时,则重新订阅 & 发送原来的 Observable

 

     4、总结