rxjava 七:zip操作符

说明

zip:压缩工具生成的压缩文件个是
此处同理:将两个或多个事件压缩成一个事件处理

代码

   Observable observable1 = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                Log.i("zqq","1");
                emitter.onNext(2);
                Log.i("zqq","2");
                emitter.onNext(3);
                Log.i("zqq","3");
                emitter.onNext(4);
                Log.i("zqq","4");
                emitter.onNext(5);
                Log.i("zqq","5");
                emitter.onComplete();
            }
        });

        Observable observable2 = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(11);
                Log.i("zqq","11");
                emitter.onNext(22);
                Log.i("zqq","22");
                emitter.onNext(33);
                Log.i("zqq","33");
                emitter.onNext(44);
                Log.i("zqq","44");
                emitter.onComplete();
            }
        });

        Observable.zip(observable1, observable2, new BiFunction() {
            @Override
            public Integer apply(Integer integer, Integer integer2) throws Exception {  //将两个observable1结合到一起,然后处理为新的结果
                return integer+integer2;
            }
        })
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("zqq","accept>>"+integer);
                    }
                });

结果:
rxjava 七:zip操作符_第1张图片
可以看到:
1、observable1 和 observalbe2被组合到了一起
2、事件是严格按照顺序组合的
3、observable1 发送了5个onnext observalbe2 发送了4个,结果产生了4条,若事件无组合,忽略
再看:
observable1事件发送完成之后,observable2才开始发送,并且开始组合
原因:两个observable在通一线程,所以执行有先后顺序,解决方法,切换线程

如下,加延时,切线程

 Observable observable1 = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                Log.i("zqq","1");
                Thread.sleep(1000);
                emitter.onNext(2);
                Log.i("zqq","2");
                Thread.sleep(1000);
                emitter.onNext(3);
                Log.i("zqq","3");
                Thread.sleep(1000);
                emitter.onNext(4);
                Log.i("zqq","4");
                Thread.sleep(1000);
                emitter.onNext(5);
                Log.i("zqq","5");
                Thread.sleep(1000);
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io());

        Observable observable2 = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(11);
                Log.i("zqq","11");
                Thread.sleep(1000);
                emitter.onNext(22);
                Log.i("zqq","22");
                Thread.sleep(1000);
                emitter.onNext(33);
                Log.i("zqq","33");
                Thread.sleep(1000);
                emitter.onNext(44);
                Log.i("zqq","44");
                Thread.sleep(1000);
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io());

        Observable.zip(observable1, observable2, new BiFunction() {
            @Override
            public Integer apply(Integer integer, Integer integer2) throws Exception {  //将两个observable1结合到一起,然后处理为新的结果
                return integer+integer2;
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.i("zqq","accept>>"+integer);
                    }
                });

结果:
rxjava 七:zip操作符_第2张图片
实际开发中,经常遇到,一个列表,一个页面,多个数据组合的问题,可以用此种方式来处理

你可能感兴趣的:(android)