RxJava2 / RxAndroid2的concat拼接多个Observable

RxJava2 / RxAndroid2的concat拼接多个Observable

 

concat操作符和merge类似,把多个Observable拼接成一个可以观察的输出,例如代码:

 

package zhangphil.app;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();
    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        test();
    }

    private void test() {

        DisposableObserver disposableObserver = new DisposableObserver() {
            @Override
            public void onNext(String s) {
                Log.d(TAG, "#####开始#####");
                Log.d(TAG + "数据", String.valueOf(s));
                Log.d(TAG, "#####结束#####");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, e.toString(), e);
            }
        };

        mCompositeDisposable.add(
                Observable.concat(
                        getObservableA(null),
                        getObservableB(null),
                        getObservableA(null),
                        getObservableB(null))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeWith(disposableObserver));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 如果退出程序,就清除后台任务
        mCompositeDisposable.clear();
    }

    private Observable getObservableA(Object o) {
        return Observable.fromCallable(new Callable() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(500); // 假设此处是耗时操作
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return "A";
            }
        });
    }

    private Observable getObservableB(Object o) {
        return Observable.fromCallable(new Callable() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(1000); // 假设此处是耗时操作
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return "B";
            }
        });
    }
}

 

输出:

 

05-15 14:39:18.667 14456-14456/zhangphil.app D/MainActivity: #####开始#####
05-15 14:39:18.667 14456-14456/zhangphil.app D/MainActivity数据: A
05-15 14:39:18.667 14456-14456/zhangphil.app D/MainActivity: #####结束#####
05-15 14:39:19.669 14456-14456/zhangphil.app D/MainActivity: #####开始#####
05-15 14:39:19.669 14456-14456/zhangphil.app D/MainActivity数据: B
05-15 14:39:19.669 14456-14456/zhangphil.app D/MainActivity: #####结束#####
05-15 14:39:20.170 14456-14456/zhangphil.app D/MainActivity: #####开始#####
05-15 14:39:20.170 14456-14456/zhangphil.app D/MainActivity数据: A
05-15 14:39:20.170 14456-14456/zhangphil.app D/MainActivity: #####结束#####
05-15 14:39:21.171 14456-14456/zhangphil.app D/MainActivity: #####开始#####
05-15 14:39:21.172 14456-14456/zhangphil.app D/MainActivity数据: B
05-15 14:39:21.172 14456-14456/zhangphil.app D/MainActivity: #####结束#####
05-15 14:39:21.172 14456-14456/zhangphil.app D/MainActivity: onComplete

 

你可能感兴趣的:(Android,RxJava,/,RxAndroid响应式编程)