Rxjava2 operator (1) Rxjava2 operator (2) Rxjava2 operator (3) Demo address :github

Error handling type

Retry: The original Observable retries when encountering an error. If the error persists, it crashes

 Observable.just(1, "2", 3)
                        .cast(Integer.class)
                        .retry(3)
                        .subscribe(new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Exception {
                                Log.i(TAG, Retry retry data+integer); }});Copy the code

It crashes after printing four ones

12-12 06:04:41. 890, 10380-10380 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: Retry retry data 1-12 of 12 06:04:41. 890, 10380-10380 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: Retry retry data 1-12 of 12 06:04:41. 891, 10380-10380 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: Retry retry data 1-12 of 12 06:04:41. 891, 10380-10380 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: Retry retry data 1-12 of 12 06:04:41. 892, 10380-10380 / niezhiyang. Cn. Rxjava2_android_samples W/System. Err: at niezhiyang.cn.rxjava2_android_samples.ErrorHandlingActivity.onViewClicked(ErrorHandlingActivity.java:32)Copy the code

RetryWhen When the original Observable encounters an error, it passes the error to another Observable to decide whether to re-subscribe to the Observable

  Observable.just(1, "2", 3)
                        .cast(Integer.class)
                        .retryWhen(new Function<Observable<Throwable>, ObservableSource<Integer>>() {
                            @Override
                            public ObservableSource<Integer> apply(Observable<Throwable> throwableObservable) throws Exception {
                                returnObservables. Just (4, 5); } }) .subscribe(new Consumer<Integer>() { @Override public void accept(Integerinteger) throws Exception {
                                Log.i(TAG, "RetryWhen retry data"+integer); }});Copy the code

Print out two ones

12-12 06:08:08. 150, 10487-10487 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: RetryWhen retry data 1-12 of 12 06:08:08. 150, 10487-10487 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: RetryWhen Retry data 1Copy the code

OnErrorReturn Emits a specific data when the original Observable encounters an error

  Observable.just(1,"2",3)
                        .cast(Integer.class)
                        .onErrorReturn(new Function<Throwable, Integer>() {
                            @Override
                            public Integer apply(Throwable throwable) throws Exception {
                                return 0;
                            }
                        }).subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i(TAG, "Default data after onErrorReturn error"+integer); }});Copy the code

It printed: 1.0

12-12 06:10:45. 547, 10487-10487 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: OnErrorReturn errors after the default data of 1-12 of 12 06:10:45. 547, 10487-10487 / niezhiyang. Cn. Rxjava2_android_samples I/ErrorHandlingActivity: OnErrorReturn The default value 0 after an error occursCopy the code

OnErrorResumeNext Returns a new Observable when an error is encountered

  • Usage scenario: When requesting data, if the token is invalid, i.e. the HTTP return code is 401, you can use this to request the token interface again and then request the data

ancillary

DoOnNext lets subscribers do something when they receive data. Let’s say we want to save the data before we get it

 Observable.just(1, 2, 3).doOnNext(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i(TAG, "Before doOnNext accepts :" + integer);
                    }
                }).subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i(TAG, Data received by doOnNext: + integer); }});Copy the code

It printed:

12-12 06:30:36. 749, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doOnNext accept before: 1-12 of 12 06:30:36. 750, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doOnNext received data: 1-12 of 12 06:30:36. 750, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doOnNext accept before: 2-12 of 12 06:30:36. 750, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doOnNext received data: 2-12 of 12 06:30:36. 750, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doOnNext accept before: 3-12 of 12 06:30:36. 750, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doData received by OnNext :3Copy the code

DoAfterNext As opposed to doOnNext is the action that takes place after data is received

  Observable.just(1, 2, 3).doAfterNext(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i(TAG, "After doAfterNext accepts :" + integer);
                    }
                }).subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i(TAG, "Data received by doAfterNext: + integer); }});Copy the code

It printed:

6:31:38. 773, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doAfterNext received data: 1-12 of 12 06:31:38. 773, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doAfterNext accept after: 1-12 of 12 06:31:38. 773, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doAfterNext received data: 2-12 of 12 06:31:38. 773, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doAfterNext accept: after 2-12 of 12 06:31:38. 773, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doAfterNext received data: 3-12 of 12 06:31:38. 773, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doAfterNext Acceptance :3Copy the code

DoAfterNext As opposed to doOnNext is the action that takes place after data is received

  Observable.just(1, 2, 3).doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.i(TAG, "Final operation.");
                    }
                }).subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i(TAG, "Data received by doOnComplete: + integer); }});Copy the code

It printed:

12-12 06:32:40. 045, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doThe OnComplete received data: 1-12 of 12 06:32:40. 045, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doThe OnComplete received data: 2-12 of 12 06:32:40. 045, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity:doThe OnComplete received data: 3-12 of 12 06:32:40. 045, 11324-11324 / niezhiyang. Cn. Rxjava2_android_samples I/UtilityActivity: the final operationCopy the code

Conditional and Boolean

All sends all the data to see if each meets the criteria, and then passes a Boolean

Observable. Just (1,2,3). All (new Predicate<Integer>() {@override public Booleantest(Integer integer) throws Exception {
                                return integer> 2. } }) .subscribe(new BiConsumer<Boolean, Throwable>() { @Override public void accept(Boolean aBoolean, Throwable throwable) throws Exception { Log.i(TAG,"all-->:"+ aBoolean); }});Copy the code

Print is

12-12 06:58:57. 958, 12153-12153 / niezhiyang. Cn. Rxjava2_android_samples I/ConditionalandBooleanActivity: all - > :false
Copy the code

Contains Determines whether the sent data contains any data, and returns true if there is one

Observables. Just (1, 2, 3). The contains (3) the subscribe (new BiConsumer < Boolean, Throwable>() { @Override public void accept(Boolean aBoolean, Throwable throwable) throws Exception { Log.i(TAG,"contains-->:"+ aBoolean); }});Copy the code

Print is

12-12 06:59:03. 123, 12153-12153 / niezhiyang. Cn. Rxjava2_android_samples I/ConditionalandBooleanActivity: the contains - > :true
Copy the code

Contains Determines whether the sent data contains any data, and returns true if there is one

Observables. Just (1, 2, 3). The contains (3) the subscribe (new BiConsumer < Boolean, Throwable>() { @Override public void accept(Boolean aBoolean, Throwable throwable) throws Exception { Log.i(TAG,"contains-->:"+ aBoolean); }});Copy the code

Print is

12-12 06:59:03. 123, 12153-12153 / niezhiyang. Cn. Rxjava2_android_samples I/ConditionalandBooleanActivity: the contains - > :true
Copy the code

IsEmpty determines if any data was sent, false if so and returns true if only OnComplete was sent

Observable. Just (1,2,3).isempty (). Subscribe (new Consumer<Boolean>() {@override public void accept(Boolean aBoolean) throws Exception { Log.i(TAG,"isEmpty -->:"+ aBoolean); }});Copy the code

Print is

12-12 07:03:42. 007, 12390-12390 / niezhiyang. Cn. Rxjava2_android_samples I/ConditionalandBooleanActivity: isEmpty - > :false
Copy the code

TakeUntil the sent data is terminated after a certain condition is met

Observable. Just (1,2,3). TakeUntil (new Predicate<Integer>() {@override public Booleantest(Integer integer) throws Exception {
                                return integer> 1; } }) .subscribe(new Consumer<Integer>() { @Override public void accept(Integerinteger) throws Exception {
                                Log.i(TAG, "takeUntil -->:" + integer); }});Copy the code

Print is

12-12 07:13:47. 657, 12964-12964 / niezhiyang. Cn. Rxjava2_android_samples I/ConditionalandBooleanActivity: TakeUntil - > : 1-12 of 12 07:13:47. 657, 12964-12964 / niezhiyang. Cn. Rxjava2_android_samples I/ConditionalandBooleanActivity: takeUntil -->:2Copy the code