RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen

1.startWith:给你被观察者的数据流前再增加一点同类型的数据或者增加一个数据流
RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen_第1张图片
startWith.png
Observable.just("a", "b", "c").startWith("呵呵", "哦哦")
                .subscribe(new Action1() {
                    @Override
                    public void call(String s) {
                        Log.e("Tag", "call: " + s);
                    }
                });

or

Observable.just("a", "b", "c")
                .startWith(Observable.from(new String[]{"呵呵", "哦哦"}))
                .subscribe(new Action1() {
                    @Override
                    public void call(String s) {
                        Log.e("Tag", "call: " + s);
                    }
                });

输出日志:
07-19 15:50:50.023 27691-27691/com.example.audiotalk E/Tag: call: 呵呵
07-19 15:50:50.023 27691-27691/com.example.audiotalk E/Tag: call: 哦哦
07-19 15:50:50.023 27691-27691/com.example.audiotalk E/Tag: call: a
07-19 15:50:50.023 27691-27691/com.example.audiotalk E/Tag: call: b
07-19 15:50:50.023 27691-27691/com.example.audiotalk E/Tag: call: c

2.merge:把多个被观察者合并到一个被观察者身上输出,但是可能会让合并的被观察者发射的数据交错。
RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen_第2张图片
merge.png
Observable.merge(Observable.just("a", "b", "c"), Observable.just("d", "e", "f"),
                               Observable.just("g", "h", "i"))
                .subscribe(s -> Log.e("Tag", "merge: " + s));

输出日志:
07-19 15:55:51.304 29657-29657/com.example.audiotalk E/Tag: merge: a
07-19 15:55:51.304 29657-29657/com.example.audiotalk E/Tag: merge: b
07-19 15:55:51.304 29657-29657/com.example.audiotalk E/Tag: merge: c
07-19 15:55:51.305 29657-29657/com.example.audiotalk E/Tag: merge: d
07-19 15:55:51.305 29657-29657/com.example.audiotalk E/Tag: merge: e
07-19 15:55:51.305 29657-29657/com.example.audiotalk E/Tag: merge: f
07-19 15:55:51.305 29657-29657/com.example.audiotalk E/Tag: merge: g
07-19 15:55:51.305 29657-29657/com.example.audiotalk E/Tag: merge: h
07-19 15:55:51.305 29657-29657/com.example.audiotalk E/Tag: merge: i

3.concat:同是合并数据,但是严格按照顺序发射,一个被观察者数据发送完前不会发送后一个被观察者的数据。所以说concat肯定是不会发生数据交错发射的情况。
RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen_第3张图片
concat.png
Observable.concat(Observable.just("a", "b", "c"), Observable.just("d", "e", "f"),
                               Observable.just("g", "h","i"))
                  .subscribe(s -> Log.e("concat", "initTest: " + s));

输出日志:
07-19 15:59:42.885 30429-30429/com.example.audiotalk E/concat: initTest: a
07-19 15:59:42.885 30429-30429/com.example.audiotalk E/concat: initTest: b
07-19 15:59:42.885 30429-30429/com.example.audiotalk E/concat: initTest: c
07-19 15:59:42.885 30429-30429/com.example.audiotalk E/concat: initTest: d
07-19 15:59:42.885 30429-30429/com.example.audiotalk E/concat: initTest: e
07-19 15:59:42.886 30429-30429/com.example.audiotalk E/concat: initTest: f
07-19 15:59:42.886 30429-30429/com.example.audiotalk E/concat: initTest: g
07-19 15:59:42.886 30429-30429/com.example.audiotalk E/concat: initTest: h
07-19 15:59:42.886 30429-30429/com.example.audiotalk E/concat: initTest: i

4.zip:合并多个被观察者发出的数据项,但是如果一个被观察者的数据更多,多出来的那部分不会被发送。(zipWith和zip类似)

此操作符适用情况:比如说我们在请求第三个接口的时候需要用到前两个接口请求成功之后返回的数据,这时候我们可能会想到使用嵌套请求,在第一个接口请求成功之后,再请求第二个接口,第二个接口请求成功之后,再请求第三个接口。但是这样嵌套起来代码可读性非常差。使用zip操作符就清晰明朗很多。
zip

RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen_第4张图片
zip.png
Observable.zip(Observable.just(1), Observable.just(2), new Func2() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                return integer+integer2;
            }
        }).subscribe(new Action1() {
            @Override
            public void call(Integer integer) {
                // TODO: 2018/7/19 0019  do next request
                
            }
        });

zipWith

Observable.just(1).zipWith( Observable.just(2), new Func2() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                return integer+integer2;
            }
        }).subscribe(new Action1() {
            @Override
            public void call(Integer integer) {
                // TODO: 2018/7/19 0020  do next request
                
            }
        });
5.combineLatest:他可以组合两个Observable,进行一定的操作之后,再次发射下去

它继续发射的前提是:其中的一个Observable还有数据没有发射,那么,他讲两个Observable目前最新发射的数据组合在一起,比如上面,第一个Observable最新的数据是8,然后第二个的依次在变,然后再把他们组合在一起。8&&10 8&&11 ...

RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen_第5张图片
combineLatest.png
Observable.combineLatest(Observable.range(7, 2),
                Observable.range(10, 4), new Func2() {
                    @Override
                    public String call(Integer integer, Integer integer2) {
                        return integer + "&&" + integer2;
                    }
                }).subscribe(new Action1() {
            @Override
            public void call(String s) {
                Log.e("combineLatest", s);
            }
        });

输出日志:
07-19 17:08:56.778 6545-6545/com.example.audiotalk E/combineLatest: 8&&10
07-19 17:08:56.778 6545-6545/com.example.audiotalk E/combineLatest: 8&&11
07-19 17:08:56.778 6545-6545/com.example.audiotalk E/combineLatest: 8&&12
07-19 17:08:56.778 6545-6545/com.example.audiotalk E/combineLatest: 8&&13

6.retryWhen:请求失败重试

下面是我自定义的一个请求失败重试的类,供参考(可根据请求失败返回的异常信息,自定义是否需要重试请求)

RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen_第6张图片
retryWhen.png

使用方法:

Observable.just(2)//这里换成自己的接口请求
                .retryWhen(new RetryWhenProcess(5, 3))//请求失败之后延时5,秒重试,总共重试次数3次
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(Integer integer) {

                    }
                }, new Action1() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

封装的请求失败重试类

public static class RetryWhenProcess implements Func1, Observable> {

        private long mInterval;//请求失败延时几秒之后重试
        private int times;//重试次数
        private int count;

        public RetryWhenProcess(long interval, int times) {
            mInterval = interval;
            this.times = times;
        }

        @Override
        public Observable call(Observable observable) {
            return observable.flatMap((Func1>) throwable -> {

                if (throwable instanceof UnknownHostException/*||throwable instanceof SocketTimeoutException*/) {
                    return Observable.error(throwable);
                }
                if (this.times <= count) {
                    return Observable.error(throwable);
                }
                count++;
                return Observable.just(Observable.timer((long) Math.pow(mInterval, 1), TimeUnit.SECONDS));
            });
        }
    }

你可能感兴趣的:(RxJava常用操作符startWith、merge、concat、zip、combineLatest、retryWhen)