RxJava2 / RxAndroid2的merge操作合并多个Observable

时间:2022-09-30 17:46:02
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数据: A05-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数据: B05-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数据: A05-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数据: B05-15 13:40:44.431 12972-12972/zhangphil.app D/MainActivity: #####结束#####