组合操作符

concat操作符

concat操作符是将多个Observable 按传入顺序进行输出,O_a输出完毕,O_b接着输出:

Observable range = Observable.range(1, 5);  
      List data = Arrays.asList(6, 7, 8, 9, 10);  
      Observable from = Observable.from(data);  
  
      Observable.concat(from,range).subscribe(new Observer() {  
          @Override  
          public void onCompleted() {  
              LogUtils.d("----->onCompleted");  
          }  
  
          @Override  
          public void onError(Throwable e) {  
              LogUtils.d("----->onError");  
          }  
  
          @Override  
          public void onNext(Integer o) {  
              LogUtils.d("----->onNext:" + o);  
          }  
      });  

运行结果如下:
----->onNext:6
----->onNext:7
----->onNext:8
----->onNext:9
----->onNext:10
----->onNext:1
----->onNext:2
----->onNext:3
----->onNext:4
----->onNext:5
----->onCompleted


combineLastest操作符

combineLastest() 是将第一个Observable的【最新】数据与后面的Observable数据项按某种规则合并。

 Observable.combineLatest(range, from, new Func2() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                Log.d("rxandroidtest","----->i1:"+integer+"  i2:"+integer2);
                return integer + integer2;
            }
        }).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                Log.d("rxandroidtest","----->onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("rxandroidtest","----->onError");
            }

            @Override
            public void onNext(Integer o) {
                Log.d("rxandroidtest","----->onNext:" + o);
            }
        });

运行结果如下:
----->i1:5 i2:6
----->onNext:11
----->i1:5 i2:7
----->onNext:12
----->i1:5 i2:8
----->onNext:13
----->i1:5 i2:9
----->onNext:14
----->i1:5 i2:10
----->onNext:15
----->onCompleted


join操作符

在a的生命周期内:b输出的数据项与a输出的数据项每个合并,直到b输出下一项:
为什么要加延迟?是因为可能b的输出会因为a的生命周期结束了而b还未开始
rang 和 from 参考上面

        from.observeOn(Schedulers.newThread());
        range.observeOn(Schedulers.newThread());
        range.join(from, new Func1>() {
            @Override
            public Observable call(Integer integer) {
                Log.d("join","left:"+integer);
                return Observable.just(integer).delay(1, TimeUnit.SECONDS);
            }
        }, new Func1>() {
            @Override
            public Observable call(Integer integer) {
                Log.d("join","right:"+integer);
                return Observable.just(integer).delay(1, TimeUnit.SECONDS);
            }
        }, new Func2() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                Log.d("join","left:"+integer + " right:"+integer2);
                return integer + integer2;
            }

        }).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                Log.d("join","onCompleted!");
            }

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

            @Override
            public void onNext(Integer o) {
                Log.d("join","onNext:"+o);
            }
        });

运行结果如下:
D/join: left:1
D/join: left:2
D/join: left:3
D/join: left:4
D/join: left:5
D/join: right:6
D/join: left:5 right:6
D/join: onNext:11
D/join: left:2 right:6
D/join: onNext:8
D/join: left:1 right:6
D/join: onNext:7
D/join: left:4 right:6
D/join: onNext:10
D/join: left:3 right:6
D/join: onNext:9
D/join: right:7
D/join: left:5 right:7
D/join: onNext:12
D/join: left:2 right:7
D/join: onNext:9
D/join: left:1 right:7
D/join: onNext:8
D/join: left:4 right:7
D/join: onNext:11
D/join: left:3 right:7
D/join: onNext:10
D/join: right:8
D/join: left:5 right:8
D/join: onNext:13
D/join: left:2 right:8
D/join: onNext:10
D/join: left:1 right:8
D/join: onNext:9
D/join: left:4 right:8
D/join: onNext:12
D/join: left:3 right:8
D/join: onNext:11
D/join: right:9
D/join: left:5 right:9
D/join: onNext:14
D/join: left:2 right:9
D/join: onNext:11
D/join: left:1 right:9
D/join: onNext:10
D/join: left:4 right:9
D/join: onNext:13
D/join: left:3 right:9
D/join: onNext:12
D/join: right:10
D/join: left:5 right:10
D/join: onNext:15
D/join: left:2 right:10
D/join: onNext:12
D/join: left:1 right:10
D/join: onNext:11
D/join: left:4 right:10
D/join: onNext:14
D/join: left:3 right:10
D/join: onNext:13
D/join: onCompleted!


merge 操作符

按照两个Observable提交结果的时间顺序进行输出

 Observable.merge(range, from).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                Log.d("join","onCompleted!");
            }

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

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

运行结果如下:
onNext:1
D/join: onNext:2
D/join: onNext:3
D/join: onNext:4
D/join: onNext:5
D/join: onNext:6
D/join: onNext:7
D/join: onNext:8
D/join: onNext:9
D/join: onNext:10
D/join: onCompleted!

例子2

  Observable interval_1 = Observable.interval(3, TimeUnit.SECONDS).map(aLong -> aLong * 1).take(5);
        Observable interval_100 = Observable.interval(3, TimeUnit.SECONDS).map(aLong -> aLong * 100).take(5);
        Observable.merge(interval_1,interval_100).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                Log.d("join","onCompleted!");
            }

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

            @Override
            public void onNext(Long aLong) {
                Log.d("join","onNext:"+aLong);
            }
        });

运行结果如下:

D/join: onNext:0
D/join: onNext:0
D/join: onNext:1
D/join: onNext:100
D/join: onNext:2
D/join: onNext:200
D/join: onNext:3
D/join: onNext:300
D/join: onNext:4
D/join: onNext:400
D/join: onCompleted!


mergeDelayError操作符

在merge过程中 一旦某一个Observable中出现错误,就会停止合 并,mergeDelayError操作符会把错误放在所有结果都和并完成后才执行:

Observable interval_1 = Observable.interval(3, TimeUnit.SECONDS).map(aLong -> {
            if(aLong > 2) throw new RuntimeException();
            return aLong * 1;
        }).take(5);
        Observable interval_100 = Observable.interval(3, TimeUnit.SECONDS).map(aLong -> aLong * 100).take(5);
        Observable.mergeDelayError(interval_1,interval_100).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                Log.d("join","onCompleted!");
            }

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

            @Override
            public void onNext(Long aLong) {
                Log.d("join","onNext:"+aLong);
            }
        });

运行结果如下:
onNext:0
onNext:0
onNext:1
onNext:100
onNext:2
onNext:200
onNext:300
onNext:400
onError:java.lang.RuntimeException


startWith操作符

在源Observable输出之前插入指定数据项:

  range.startWith(8,9).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                Log.d("join","onCompleted!");
            }

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

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

        });

运行结果如下:
onNext:8
onNext:9
onNext:1
onNext:2
onNext:3
onNext:4
onNext:5
onCompleted!


switchOnNext操作符

把一组Observable转换成一个Observable, 这组Observable中的每一个Observable所产生的结果,如果在同一个时间段内两个或者多个Observable提交结果,只取最后一个Observable提交的结果

 Observable> longObservable = Observable.interval(10, TimeUnit.SECONDS).map(new Func1>() {
            @Override
            public Observable call(final Long aLong1) {
                return Observable.interval(1, TimeUnit.SECONDS).map(new Func1() {
                    @Override
                    public Long call(Long aLong) {
                    //    Log.d("join","------>aLong1:" + aLong);
                    //    Log.d("join","------>aLong:" + aLong);
                        return aLong + (aLong1*100);
                    }
                }).take(3);
            }
        }).take(3);

        Observable.switchOnNext(longObservable).subscribe(new Observer() {
            @Override
            public void onCompleted() {
                Log.d("join","------>onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("join","------>onError()" + e);
            }

            @Override
            public void onNext(Long integer) {
                Log.d("join","------>onNext()" + integer);
            }
        });

运行结果如下:

onNext()0
onNext()1
onNext()2
onNext()100
onNext()101
onNext()102
onNext()200
onNext()201
onNext()202
onCompleted()
如果同一时间有多个Observable发射,只取最后一个

  Observable> longObservable = Observable.interval(3, TimeUnit.SECONDS).map(new Func1>() {
            @Override
            public Observable call(final Long aLong1) {
                return Observable.interval(1, TimeUnit.SECONDS).map(new Func1() {
                    @Override
                    public Long call(Long aLong) {
                        //    Log.d("join","------>aLong1:" + aLong);
                        //    Log.d("join","------>aLong:" + aLong);
                        return aLong + (aLong1*100);
                    }
                }).take(3);
            }
        }).take(3);

        Observable.switchOnNext(longObservable).subscribe(new Observer() {
            @Override
            public void onCompleted() {
                Log.d("join","------>onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("join","------>onError()" + e);
            }

            @Override
            public void onNext(Long integer) {
                Log.d("join","------>onNext()" + integer);
            }
        });

运行结果如下:
少了2,102
onNext()0
onNext()1
onNext()100
onNext()101
onNext()200
onNext()201
onNext()202
onCompleted()


zip 操作符

是将同索引位置的数据项进行合并,放单的数据项丢弃

 Observable.zip(Observable.just(1, 2, 3), Observable.just(10, 20, 30), new Func2() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        }).subscribe(
                new Observer() {
                    @Override
                    public void onCompleted() {
                        Log.d("join","------>onCompleted()");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("join","------>onError()" + e);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d("join","------>onNext()" + integer);
                    }
                }
        );

运行结果如下:

onNext()11
onNext()22
onNext()33
onCompleted()


你可能感兴趣的:(组合操作符)