本文主要是介绍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();@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);test();}private void test() {DisposableObserver disposableObserver = new DisposableObserver<String>() {@Overridepublic void onNext(String s) {Log.d(TAG, "#####开始#####");Log.d(TAG + "数据", String.valueOf(s));Log.d(TAG, "#####结束#####");}@Overridepublic void onComplete() {Log.d(TAG, "onComplete");}@Overridepublic 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));}@Overrideprotected void onDestroy() {super.onDestroy();// 如果退出程序,就清除后台任务mCompositeDisposable.clear();}private Observable<String> getObservableA(Object o) {return Observable.fromCallable(new Callable<String>() {@Overridepublic 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>() {@Overridepublic 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
这篇关于RxJava2 / RxAndroid2的concat拼接多个Observable的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!