RxJava進階四(組合類操作符)

RxJava進階

RxJava進階一(創(chuàng)建類操作符)
RxJava進階二(轉換類操作符)
RxJava進階三(過濾類操作符)
RxJava進階四(組合類操作符)


前言

本篇文章帶著大家熟悉一下RxJava的組合類操作符,本系列文章僅是帶大家認識一下這些操作符的用法,并沒有對操作符進行多種形態(tài)的使用,具體的還需要大家在使用時注意~

操作符總覽

CombineLatest、Join、Merge、StartWith、Switch、Zip...

具體使用介紹

CombineLatest

當兩個Observables中的任何一個發(fā)射了一個數(shù)據(jù)時,將兩個Observables數(shù)據(jù)通過指定的規(guī)則進行處理,將結果進行發(fā)射~

代碼示例:

        Observable<Long> observable1 = Observable.interval(0, 500, TimeUnit.MILLISECONDS).take(3);
        Observable<Long> observable2 = Observable.interval(500, 500, TimeUnit.MILLISECONDS).take(3);

        Observable.combineLatest(observable1, observable2, new Func2<Long, Long, Long>() {
            @Override
            public Long call(Long along1, Long along2) {
                System.out.println("along1 --> " + along1);
                System.out.println("along2 --> " + along2);
                return along1 + along2;
            }
        }).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                System.out.println("result = " + aLong);
                System.out.println("--------------");
            }
        });

運行結果:

10-08 17:00:12.319 6650-6698/com.shenghan.haobaobei I/System.out: along1 --> 1
10-08 17:00:12.319 6650-6698/com.shenghan.haobaobei I/System.out: along2 --> 0
10-08 17:00:12.319 6650-6698/com.shenghan.haobaobei I/System.out: result = 1
10-08 17:00:12.319 6650-6698/com.shenghan.haobaobei I/System.out: --------------
10-08 17:00:12.816 6650-6728/com.shenghan.haobaobei I/System.out: along1 --> 2
10-08 17:00:12.816 6650-6728/com.shenghan.haobaobei I/System.out: along2 --> 0
10-08 17:00:12.816 6650-6728/com.shenghan.haobaobei I/System.out: result = 2
10-08 17:00:12.817 6650-6728/com.shenghan.haobaobei I/System.out: --------------
10-08 17:00:12.818 6650-6698/com.shenghan.haobaobei I/System.out: along1 --> 2
10-08 17:00:12.820 6650-6698/com.shenghan.haobaobei I/System.out: along2 --> 1
10-08 17:00:12.821 6650-6698/com.shenghan.haobaobei I/System.out: result = 3
10-08 17:00:12.821 6650-6698/com.shenghan.haobaobei I/System.out: --------------
10-08 17:00:13.315 6650-6698/com.shenghan.haobaobei I/System.out: along1 --> 2
10-08 17:00:13.315 6650-6698/com.shenghan.haobaobei I/System.out: along2 --> 2
10-08 17:00:13.315 6650-6698/com.shenghan.haobaobei I/System.out: result = 4
10-08 17:00:13.315 6650-6698/com.shenghan.haobaobei I/System.out: --------------

結論:

** 1.只有當兩個observable都發(fā)射過第一項數(shù)據(jù)時,才能進行組合發(fā)射**

從log日志來看,observable1打印的第一項數(shù)據(jù)為1,所以可以推斷observable1發(fā)射的0數(shù)據(jù)時,observable2并沒有發(fā)射過數(shù)據(jù),所以并沒有進行組合。

** 2.當observable1與observable2都發(fā)射首個數(shù)據(jù)后,在此發(fā)射任何數(shù)據(jù)都會找相應的另外一個observable的最新數(shù)據(jù)進行組合發(fā)射**

Join

無論何時,如果一個Observable發(fā)射了一個數(shù)據(jù)項,只要在另一個Observable發(fā)射的數(shù)據(jù)項定義的時間窗口內(nèi),就將兩個Observable發(fā)射的數(shù)據(jù)合并發(fā)射~

        Observable<Long> observable1 = Observable.interval(2000, 1000, TimeUnit.MILLISECONDS).take(3);
        Observable<Long> observable2 = Observable.interval(2500, 1000, TimeUnit.MILLISECONDS).take(3);

        observable1
                .join(
                        observable2,
                        new Func1<Long, Observable<Long>>() {
                            @Override
                            public Observable<Long> call(Long aLong) {
                                //使Observable延遲500毫秒執(zhí)行
                                System.out.println("observable1 -- >" + aLong);
                                return Observable.just(aLong).delay(500, TimeUnit.MILLISECONDS);
                            }
                        },
                        new Func1<Long, Observable<Long>>() {
                            @Override
                            public Observable<Long> call(Long aLong) {
                                System.out.println("observable2 -- >" + aLong);
                                return Observable.just(aLong).delay(500, TimeUnit.MILLISECONDS);
                            }
                        },
                        new Func2<Long, Long, Long>() {
                            @Override
                            public Long call(Long aLong1, Long aLong2) {
                                System.out.println("aLong1 = " + aLong1);
                                System.out.println("aLong2 = " + aLong2);
                                return aLong1 + aLong2;
                            }
                        })
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        System.out.println("result = " + aLong);
                        System.out.println("--------------");
                    }
                });

運行結果:

10-08 16:47:56.988 20959-21011/com.shenghan.haobaobei I/System.out: observable1 -- >0
10-08 16:47:57.488 20959-21041/com.shenghan.haobaobei I/System.out: observable2 -- >0
10-08 16:47:57.488 20959-21041/com.shenghan.haobaobei I/System.out: aLong1 = 0
10-08 16:47:57.488 20959-21041/com.shenghan.haobaobei I/System.out: aLong2 = 0
10-08 16:47:57.488 20959-21041/com.shenghan.haobaobei I/System.out: result = 0
10-08 16:47:57.488 20959-21041/com.shenghan.haobaobei I/System.out: --------------
10-08 16:47:57.991 20959-21011/com.shenghan.haobaobei I/System.out: observable1 -- >1
10-08 16:47:57.991 20959-21011/com.shenghan.haobaobei I/System.out: aLong1 = 1
10-08 16:47:57.991 20959-21011/com.shenghan.haobaobei I/System.out: aLong2 = 0
10-08 16:47:57.991 20959-21011/com.shenghan.haobaobei I/System.out: result = 1
10-08 16:47:57.991 20959-21011/com.shenghan.haobaobei I/System.out: --------------
10-08 16:47:58.490 20959-21041/com.shenghan.haobaobei I/System.out: observable2 -- >1
10-08 16:47:58.490 20959-21041/com.shenghan.haobaobei I/System.out: aLong1 = 1
10-08 16:47:58.490 20959-21041/com.shenghan.haobaobei I/System.out: aLong2 = 1
10-08 16:47:58.490 20959-21041/com.shenghan.haobaobei I/System.out: result = 2
10-08 16:47:58.490 20959-21041/com.shenghan.haobaobei I/System.out: --------------
10-08 16:47:58.990 20959-21011/com.shenghan.haobaobei I/System.out: observable1 -- >2
10-08 16:47:58.990 20959-21011/com.shenghan.haobaobei I/System.out: aLong1 = 2
10-08 16:47:58.990 20959-21011/com.shenghan.haobaobei I/System.out: aLong2 = 1
10-08 16:47:58.990 20959-21011/com.shenghan.haobaobei I/System.out: result = 3
10-08 16:47:58.990 20959-21011/com.shenghan.haobaobei I/System.out: --------------
10-08 16:47:59.489 20959-21041/com.shenghan.haobaobei I/System.out: observable2 -- >2
10-08 16:47:59.489 20959-21041/com.shenghan.haobaobei I/System.out: aLong1 = 2
10-08 16:47:59.489 20959-21041/com.shenghan.haobaobei I/System.out: aLong2 = 2
10-08 16:47:59.489 20959-21041/com.shenghan.haobaobei I/System.out: result = 4
10-08 16:47:59.489 20959-21041/com.shenghan.haobaobei I/System.out: --------------

結論:

** 1.只有當兩個observable都發(fā)射過第一項數(shù)據(jù)時,才能進行組合發(fā)射**

從log日志來看,observable1發(fā)射數(shù)據(jù)0時,組合的call方法并沒有被執(zhí)行,而當observable2發(fā)射數(shù)據(jù)0后,組合call方法被回調(diào)了。

** 2.當observable1與observable2都發(fā)射首個數(shù)據(jù)后,在此發(fā)射任何數(shù)據(jù)都會找相應的另外一個observable的最新數(shù)據(jù)進行組合發(fā)射**

Merge##

將兩個Observable發(fā)射的數(shù)據(jù)按照時間順序進行組合,合并成一個Observable進行發(fā)射~

代碼示例:

        Observable<String> observable1 = Observable.interval(0, 500, TimeUnit.MILLISECONDS).take(3).flatMap(new Func1<Long, Observable<String>>() {
            @Override
            public Observable<String> call(Long aLong) {
                return Observable.just("observable1 -- >" + aLong);
            }
        });
        Observable<String> observable2 = Observable.interval(500, 500, TimeUnit.MILLISECONDS).take(3).flatMap(new Func1<Long, Observable<String>>() {
            @Override
            public Observable<String> call(Long aLong) {
                return Observable.just("observable2 -- >" + aLong);
            }
        });

        Observable
                .merge(observable1, observable2)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String aString) {
                        System.out.println("result = " + aString);
                    }
                });

運行結果:

10-08 17:11:33.696 16661-16737/com.shenghan.haobaobei I/System.out: result = observable1 -- >0
10-08 17:11:34.194 16661-16737/com.shenghan.haobaobei I/System.out: result = observable1 -- >1
10-08 17:11:34.198 16661-16711/com.shenghan.haobaobei I/System.out: result = observable2 -- >0
10-08 17:11:34.695 16661-16737/com.shenghan.haobaobei I/System.out: result = observable1 -- >2
10-08 17:11:34.698 16661-16711/com.shenghan.haobaobei I/System.out: result = observable2 -- >1
10-08 17:11:35.196 16661-16711/com.shenghan.haobaobei I/System.out: result = observable2 -- >2

StartWith##

在源Observable發(fā)射數(shù)據(jù)之前,先發(fā)射一個指定的數(shù)據(jù)序列或數(shù)據(jù)項~

代碼示例:

        Observable
                .just(4, 5, 6)
                .startWith(1, 2, 3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        System.out.println("result = " + integer);
                    }
                });

運行結果:

10-08 17:16:43.077 21830-21830/com.shenghan.haobaobei I/System.out: result = 1
10-08 17:16:43.078 21830-21830/com.shenghan.haobaobei I/System.out: result = 2
10-08 17:16:43.078 21830-21830/com.shenghan.haobaobei I/System.out: result = 3
10-08 17:16:43.078 21830-21830/com.shenghan.haobaobei I/System.out: result = 4
10-08 17:16:43.078 21830-21830/com.shenghan.haobaobei I/System.out: result = 5
10-08 17:16:43.078 21830-21830/com.shenghan.haobaobei I/System.out: result = 6

switchOnNext##

把一組Observable轉換成一個Observable,如果在同一個時間內(nèi)產(chǎn)生兩個或多個Observable產(chǎn)生的數(shù)據(jù),只發(fā)射最后一個Observable產(chǎn)生的數(shù)據(jù)~

示例代碼:

        Observable<Observable<String>> observable = Observable.interval(2000, 500, TimeUnit.MILLISECONDS).map(new Func1<Long, Observable<String>>() {
            @Override
            public Observable<String> call(Long aLongOutside) {
                //每隔250毫秒產(chǎn)生一組數(shù)據(jù)(0,1,2,3,4)
                return Observable.interval(0, 250, TimeUnit.MILLISECONDS).map(new Func1<Long, String>() {
                    @Override
                    public String call(Long aLongInside) {
                        return "aLongOutside = " + aLongOutside + "| aLongInside = " + aLongInside;
                    }
                }).take(5);
            }
        }).take(2);

        Observable.switchOnNext(observable).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println("result = " + s);
            }
        });

運行結果:

10-08 18:20:14.605 2921-2993/com.shenghan.haobaobei I/System.out: result = aLongOutside = 0| aLongInside = 0
10-08 18:20:14.857 2921-2993/com.shenghan.haobaobei I/System.out: result = aLongOutside = 0| aLongInside = 1
10-08 18:20:15.103 2921-3055/com.shenghan.haobaobei I/System.out: result = aLongOutside = 1| aLongInside = 0
10-08 18:20:15.353 2921-3055/com.shenghan.haobaobei I/System.out: result = aLongOutside = 1| aLongInside = 1
10-08 18:20:15.604 2921-3055/com.shenghan.haobaobei I/System.out: result = aLongOutside = 1| aLongInside = 2
10-08 18:20:15.853 2921-3055/com.shenghan.haobaobei I/System.out: result = aLongOutside = 1| aLongInside = 3
10-08 18:20:16.104 2921-3055/com.shenghan.haobaobei I/System.out: result = aLongOutside = 1| aLongInside = 4

Zip##

使用一個指定的函數(shù)將多個Observable發(fā)射的數(shù)據(jù)組合在一起,然后將這個函數(shù)的結果作為單項數(shù)據(jù)發(fā)射,嚴格周期順序進行合并,不能單獨發(fā)射~

示例代碼:

        Observable<String> observable1 = Observable.just("walid - 1", "walid - 2", "walid - 3");
        Observable<String> observable2 = Observable.just("Jordan - 1", "Jordan - 2", "Jordan - 3", "Jordan - 4");
        Observable.zip(observable1, observable2, new Func2<String, String, String>() {
            @Override
            public String call(String s1, String s2) {
                return s1 + " | " + s2;
            }
        }).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                System.err.println("onError");
            }

            @Override
            public void onNext(String value) {
                System.out.println("onNext --> " + value);
            }
        });

運行結果:

10-08 18:53:31.772 24793-24793/com.shenghan.haobaobei I/System.out: onNext --> walid - 1 | Jordan - 1
10-08 18:53:31.772 24793-24793/com.shenghan.haobaobei I/System.out: onNext --> walid - 2 | Jordan - 2
10-08 18:53:31.772 24793-24793/com.shenghan.haobaobei I/System.out: onNext --> walid - 3 | Jordan - 3
10-08 18:53:31.772 24793-24793/com.shenghan.haobaobei I/System.out: onCompleted

結論:

** 1.只有每個observable都依次產(chǎn)品一輪數(shù)據(jù)時,才會統(tǒng)一發(fā)射一次,當不會有完整一輪數(shù)據(jù)時,視為完成**

從log日志來看,observable1產(chǎn)生的數(shù)據(jù)是與observable2一一對應的,也就是只有observable1與observable2同時產(chǎn)生數(shù)據(jù)時才會統(tǒng)一發(fā)射一次~

** 2.當不滿足所有observable都有數(shù)據(jù)可產(chǎn)品時,視為完成狀態(tài)**

從log日志來看,observable2最后一項數(shù)據(jù)“Jordan - 4”并沒有打印,原因是observable1并沒有數(shù)據(jù)可以產(chǎn)生了,所以不滿足發(fā)射條件,視為完成狀態(tài)~

結語

組合類操作符,就簡單介紹到這里,希望能夠對同學有所幫助,謝謝~

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 本篇文章介主要紹RxJava中操作符是以函數(shù)作為基本單位,與響應式編程作為結合使用的,對什么是操作、操作符都有哪些...
    嘎啦果安卓獸閱讀 2,994評論 0 10
  • 創(chuàng)建操作 用于創(chuàng)建Observable的操作符Create通過調(diào)用觀察者的方法從頭創(chuàng)建一個ObservableEm...
    rkua閱讀 1,962評論 0 1
  • 注:只包含標準包中的操作符,用于個人學習及備忘參考博客:http://blog.csdn.net/maplejaw...
    小白要超神閱讀 2,381評論 2 8
  • 作者寄語 很久之前就想寫一個專題,專寫Android開發(fā)框架,專題的名字叫 XXX 從入門到放棄 ,沉淀了這么久,...
    戴定康閱讀 7,749評論 13 85
  • 作者: maplejaw本篇只解析標準包中的操作符。對于擴展包,由于使用率較低,如有需求,請讀者自行查閱文檔。 創(chuàng)...
    maplejaw_閱讀 46,234評論 8 93

友情鏈接更多精彩內(nèi)容