RxJava2.X版本-Concat语法

一、Concat语法

    public Observable getRxJavaCreateExampleData() {
        return Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 1);
                emitter.onNext(1);

                LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 2);
                emitter.onNext(2);
                Thread.sleep(5000);
                LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 3);
                emitter.onNext(3);
                emitter.onComplete();
                LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 4);
                emitter.onNext(4);

            }
        });
    }

    public Observable getRxJavaStringData() {
        return Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                LogUtils.debug(TAG, "getRxJavaStringData---:" + Thread.currentThread().getName() + "--:A");
                emitter.onNext("A");
                LogUtils.debug(TAG, "getRxJavaStringData---:" + Thread.currentThread().getName() + "--:B");
                emitter.onNext("B");
                LogUtils.debug(TAG, "getRxJavaStringData---:" + Thread.currentThread().getName() + "--:C");
                emitter.onNext("C");
            }
        });
    }

    public void rxJavaConcatExample() {
        Disposable disposable = Observable.concat(model.getRxJavaCreateExampleData().subscribeOn(Schedulers.io()), model.getRxJavaStringData().subscribeOn(Schedulers.io()))
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Serializable serializable) throws Exception {
                        LogUtils.error(TAG, "rxJavaConcatExample--Consumer--:" + Thread.currentThread().getName() + "--:" + serializable);
                    }
                });
        compositeDisposable.add(disposable);
    }

日志

08-18 17:14:33.682 6171-6593/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:1
08-18 17:14:33.683 6171-6593/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:2
08-18 17:14:33.694 6171-6171/com.example.zhang E/MainPresenter: rxJavaConcatExample--Consumer--:main--:1
    rxJavaConcatExample--Consumer--:main--:2
08-18 17:14:38.684 6171-6593/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:3
08-18 17:14:38.686 6171-6171/com.example.zhang E/MainPresenter: rxJavaConcatExample--Consumer--:main--:3
08-18 17:14:38.687 6171-6593/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:4
08-18 17:14:38.689 6171-6594/com.example.zhang D/MainModel: getRxJavaStringData---:RxCachedThreadScheduler-2--:A
08-18 17:14:38.690 6171-6594/com.example.zhang D/MainModel: getRxJavaStringData---:RxCachedThreadScheduler-2--:B
08-18 17:14:38.690 6171-6171/com.example.zhang E/MainPresenter: rxJavaConcatExample--Consumer--:main--:A
08-18 17:14:38.691 6171-6594/com.example.zhang D/MainModel: getRxJavaStringData---:RxCachedThreadScheduler-2--:C
08-18 17:14:38.691 6171-6171/com.example.zhang E/MainPresenter: rxJavaConcatExample--Consumer--:main--:B
08-18 17:14:38.692 6171-6171/com.example.zhang E/MainPresenter: rxJavaConcatExample--Consumer--:main--:C

二、总结

1、发射器 B 把自己的数据按顺序送给了发射器 A,让他们组合成了一个新的发射器
2、A发射器把数据发送完毕,B发射器的数据才开始发送

你可能感兴趣的:(RxJava2.X版本-Concat语法)