看完不懂Rxjava我跪搓板(3)

作者: 付凯强 | 来源:发表于2018-05-19 11:48 被阅读17次

    0. 为了月薪1.8万

    奋斗方向和目标.png

    1. 序言

    2. 组合操作符

    • startWith:在源Observable数据前插上一些数据:
      Observable.just("Hello", "World", "Hello").startWith("Beijing", "上海", "广州")
                    .subscribe(new Action1<String>() {
                        @Override
                        public void call(String s) {
                            Log.i(TAG, "Action1: String:" + s);
                        }
                    });
    
    05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:Beijing
    05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:上海
    05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:广州
    05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:Hello
    05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:World
    05-18 20:46:32.344 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:Hello
    
    • merge:
    1. 将多个Observable合并为一个Observable。
    Observable<String> obs1 = Observable.just("A", "B", "C");
            Observable<String> obs2 = Observable.just("D", "E", "F");
            Observable<String> obs3 = Observable.just("G", "H", "I");
            Observable<String> obs4 = Observable.just("G", "K", "L");
            Observable.merge(obs1,obs2,obs3,obs4).subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.i(TAG, "Action1: String:" + s);
                }
            });
    
    05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:A
    05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:B
    05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:C
    05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:D
    05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:E
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:F
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:G
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:H
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:I
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:G
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:K
    05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:L
    
    1. merge合并可能发生错乱(Observable订阅的线程不同时)
    Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
            Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
            Observable<String> obs3 = Observable.just("G", "H", "I");
            Observable.merge(obs1,obs2,obs3).subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.i(TAG, "Action1: String:" + s);
                }
            });
    
    05-18 21:21:44.646 2489-2489/? I/MainActivity: Action1: String:G
    05-18 21:21:44.646 2489-2489/? I/MainActivity: Action1: String:H
    05-18 21:21:44.646 2489-2489/? I/MainActivity: Action1: String:I
    05-18 21:21:44.646 2489-2505/? I/MainActivity: Action1: String:A
    05-18 21:21:44.647 2489-2505/? I/MainActivity: Action1: String:B
    05-18 21:21:44.647 2489-2505/? I/MainActivity: Action1: String:C
    05-18 21:21:44.648 2489-2505/? I/MainActivity: Action1: String:D
    05-18 21:21:44.648 2489-2505/? I/MainActivity: Action1: String:E
    05-18 21:21:44.648 2489-2505/? I/MainActivity: Action1: String:F
    
    • concat:将多个Observable发射的数据进行合并,而且不会乱序:
     Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
            Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
            Observable<String> obs3 = Observable.just("G", "H", "I");
            Observable.concat(obs1,obs2,obs3).subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.i(TAG, "Action1: String:" + s);
                }
            });
    
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:A
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:B
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:C
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:D
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:E
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:F
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:G
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:H
    05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:I
    
    • zip:合并两个或多个Observable发射出的数据项,根据指定的函数变换他们,并发射一个新值。
    Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
            Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
            Observable.zip(obs1, obs2, new Func2<String, String, String>() {
                @Override
                public String call(String s, String s2) {
                    return s + s2;
                }
            }).subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.d(TAG, "zip: " + s);
                }
            });
    
    05-18 22:50:14.022 3063-3079/com.best.testrxjava D/MainActivity: zip: AD
    05-18 22:50:14.022 3063-3079/com.best.testrxjava D/MainActivity: zip: BE
    05-18 22:50:14.022 3063-3079/com.best.testrxjava D/MainActivity: zip: CF
    
    • combineLastest:
    Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
            Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
            Observable.combineLatest(obs1, obs2, new Func2<String, String, String>() {
                @Override
                public String call(String s, String s2) {
                    return s + s2;
                }
            }).subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.d(TAG, "combineLatest: " + s);
                }
            });
    
    05-18 22:56:52.381 3241-3258/com.best.testrxjava D/MainActivity: combineLatest: CD
    05-18 22:56:52.381 3241-3258/com.best.testrxjava D/MainActivity: combineLatest: CE
    05-18 22:56:52.381 3241-3258/com.best.testrxjava D/MainActivity: combineLatest: CF
    

    说明:与zip不同的是:

    1. combineLastest结合最新的数进行合并,比如obs1中的"C"是最近发射的,最近发射的和obs2中的所有数据进行合并。
    2. zip指的是多个Observable的合并处理,而combineLastest指的是两个Observable的合并处理。

    3. 推荐博客:

    如果Recyclerview基础不太好,欢迎跳转阅读 https://www.jianshu.com/p/3eb81f50f4db

    4. 后续

    如果大家喜欢这篇文章,欢迎点赞;如果想看更多前端移动端后端Java或Python方面的技术,欢迎关注!

    相关文章

      网友评论

      • e156012376cf:好难啊 根本学不会的样子
        付凯强:@九星九微 故意找事是吧,欺负我是不是🦁🐯🐮🐷🐸🐙🐰🐻🐹🐱🤡😜
        付凯强:@九星九微 这只是Rxjava1,现在可以去看看Rxjava2 其实只是JAVA8支持流之后,它自己封装的,也可以看看JAVA8,没什么的。无非是简洁的嵌套的异步的处理数据而已。
      • 恨自己不能小清新:大佬 rxjava系列完结了吗
        恨自己不能小清新:@剑走偏锋雨 好的谢谢
        付凯强:@恨自己不能小清新 想更新,可惜工作太忙;Rxjava 建议去购买 RxJava 2.x 实战 ,有需要交流的私信我就可以了

      本文标题:看完不懂Rxjava我跪搓板(3)

      本文链接:https://www.haomeiwen.com/subject/bmhodftx.html