1. 程式人生 > >RxJava2 / RxAndroid2的merge操作合併多個Observable

RxJava2 / RxAndroid2的merge操作合併多個Observable

RxJava2/RxAndroid2的merge操作合併多個Observable


RxAndroid2/RxJava2的merge操作合併若干個Observable為單個可觀測的Observable,形成單一線性的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<String>() {
            @Override
            public void onNext(String s) {
                Log.d(TAG, "#####開始#####");
                Log.d(TAG + "資料", String.valueOf(s));
                Log.d(TAG, "#####結束#####");
            }

            @Override
            public void onComplete() {

            }

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

        mCompositeDisposable.add(
                Observable.merge(
                        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<String> getObservableA(Object o) {
        return Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(500); // 假設此處是耗時操作
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return "A";
            }
        });
    }

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

                return "B";
            }
        });
    }
}




程式碼輸出結果:
05-15 13:40:41.929 12972-12972/zhangphil.app D/MainActivity: #####開始#####
05-15 13:40:41.929 12972-12972/zhangphil.app D/MainActivity資料: A
05-15 13:40:41.929 12972-12972/zhangphil.app D/MainActivity: #####結束#####
05-15 13:40:42.930 12972-12972/zhangphil.app D/MainActivity: #####開始#####
05-15 13:40:42.930 12972-12972/zhangphil.app D/MainActivity資料: B
05-15 13:40:42.930 12972-12972/zhangphil.app D/MainActivity: #####結束#####
05-15 13:40:43.430 12972-12972/zhangphil.app D/MainActivity: #####開始#####
05-15 13:40:43.431 12972-12972/zhangphil.app D/MainActivity資料: A
05-15 13:40:43.431 12972-12972/zhangphil.app D/MainActivity: #####結束#####
05-15 13:40:44.431 12972-12972/zhangphil.app D/MainActivity: #####開始#####
05-15 13:40:44.431 12972-12972/zhangphil.app D/MainActivity資料: B
05-15 13:40:44.431 12972-12972/zhangphil.app D/MainActivity: #####結束#####