rxjava 九:其他操作符

concat 合并两个observable

       Observable observable1 = Observable.create(new ObservableOnSubscribe() {

            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onNext(4);
                emitter.onComplete();
            }
        });

        Observable observable2 = Observable.create(new ObservableOnSubscribe() {

            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(3);
                emitter.onNext(5);
                emitter.onNext(6);
                emitter.onComplete();
            }
        });


        Observable.concat(observable1,observable2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i("zqq","onSubscribe");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i("zqq","onNext>>"+integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zqq","onError");
                    }

                    @Override
                    public void onComplete() {
                        Log.i("zqq","onComplete");
                    }
                });

结果
rxjava 九:其他操作符_第1张图片
第一个observable执行完成,执行第二个
使用场景,查看缓存,缓存存在,使用缓存,不存在请求网络数据

distinct 去重

   Observable observable1 = Observable.create(new ObservableOnSubscribe() {

            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onNext(4);
                emitter.onComplete();
            }
        });

        Observable observable2 = Observable.create(new ObservableOnSubscribe() {

            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(3);
                emitter.onNext(5);
                emitter.onNext(7);
                emitter.onComplete();
            }
        });


        Observable.concat(observable1,observable2)  //合并
                .distinct()   //去重
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i("zqq","onSubscribe");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i("zqq","onNext>>"+integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zqq","onError");
                    }

                    @Override
                    public void onComplete() {
                        Log.i("zqq","onComplete");
                    }
                });

结果:
rxjava 九:其他操作符_第2张图片

filter 过滤

 Observable observable1 = Observable.create(new ObservableOnSubscribe() {

            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onNext(4);
                emitter.onComplete();
            }
        });

        Observable observable2 = Observable.create(new ObservableOnSubscribe() {

            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(3);
                emitter.onNext(5);
                emitter.onNext(7);
                emitter.onComplete();
            }
        });


        Observable.concat(observable1,observable2)  //合并
                .distinct()   //去重
                .filter(new Predicate() {  //过滤
                    @Override
                    public boolean test(Integer integer) throws Exception {
                        return integer>3;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i("zqq","onSubscribe");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i("zqq","onNext>>"+integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("zqq","onError");
                    }

                    @Override
                    public void onComplete() {
                        Log.i("zqq","onComplete");
                    }
                });

rxjava 九:其他操作符_第3张图片

interval 时间间隔执行

  // 第一次执行延时、时间间隔,间隔单位
        Observable.interval(10,5, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.i("zqq","aLong>>"+aLong);
                    }
                });

rxjava 九:其他操作符_第4张图片

你可能感兴趣的:(android,java)