串行有序
concat 连接最多四个 ObservableSource
concatWith 与 concatArray 是 concat 的升级版 , 支持连接多个ObservableSource,只是写法不同
并行无序
merge 连接最多四个 ObservableSource
mergeWith 与 mergeArray 是 merge 的升级版 , 支持连接多个ObservableSource,只是写法不同
注意:
ObservableSource error之后都不会再走下一个 ObservableSource
示例:
public void concatWith1() {
Observable<Integer> onError = getErrorObservable();
Observable<Integer> onComplete = getCompleteObservable();
onComplete.concatWith(onError)
.subscribe(getObserver("concatWith1"));
}
//concatWith1=====onSubscribe=====
//concatWith1=====onNext=====4
//concatWith1=====onNext=====5
//concatWith1=====onNext=====6
//concatWith1=====onNext=====1
//concatWith1=====onNext=====2
//concatWith1=====onNext=====3
//concatWith1=====onError=====-error-
public void concatWith2() {
Observable<Integer> onError = getErrorObservable();
Observable<Integer> onComplete = getCompleteObservable();
onError.concatWith(onComplete)
.subscribe(getObserver("concatWith2"));
}
//concatWith2=====onSubscribe=====
//concatWith2=====onNext=====1
//concatWith2=====onNext=====2
//concatWith2=====onNext=====3
//concatWith2=====onError=====-error-
public void concatArray1() {
Observable<Integer> onError = getErrorObservable();
Observable<Integer> onComplete = getCompleteObservable();
Observable.concatArray(onComplete, onError)
.subscribe(getObserver("concatArray1"));
}
//concatArray1=====onSubscribe=====
//concatArray1=====onNext=====4
//concatArray1=====onNext=====5
//concatArray1=====onNext=====6
//concatArray1=====onNext=====1
//concatArray1=====onNext=====2
//concatArray1=====onNext=====3
//concatArray1=====onError=====-error-
public void concatArray2() {
Observable<Integer> onError = getErrorObservable();
Observable<Integer> onComplete = getCompleteObservable();
Observable.concatArray(onError, onComplete)
.subscribe(getObserver("concatArray2"));
}
//concatArray2=====onSubscribe=====
//concatArray2=====onNext=====1
//concatArray2=====onNext=====2
//concatArray2=====onNext=====3
//concatArray2=====onError=====-error-
public void mergeArray1() {
Observable<Integer> onError = getErrorObservable();
Observable<Integer> onComplete = getCompleteObservable();
Observable.mergeArray(onComplete, onError)
.subscribe(getObserver("mergeArray1"));
}
//mergeArray1=====onSubscribe=====
//mergeArray1=====onNext=====4
//mergeArray1=====onNext=====5
//mergeArray1=====onNext=====6
//mergeArray1=====onNext=====1
//mergeArray1=====onNext=====2
//mergeArray1=====onNext=====3
//mergeArray1=====onError=====-error-
public void mergeArray2() {
Observable<Integer> onError = getErrorObservable();
Observable<Integer> onComplete = getCompleteObservable();
Observable.mergeArray(onError, onComplete)
.subscribe(getObserver("mergeArray2"));
}
//mergeArray2=====onSubscribe=====
//mergeArray2=====onNext=====1
//mergeArray2=====onNext=====2
//mergeArray2=====onNext=====3
//mergeArray2=====onError=====-error-
private Observer<Integer> getObserver(final String tag) {
return new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
System.out.println(tag + "=====onSubscribe=====");
}
@Override
public void onNext(Integer integer) {
System.out.println(tag + "=====onNext=====" + integer);
}
@Override
public void onError(Throwable e) {
System.out.println(tag + "=====onError=====" + e.getMessage());
}
@Override
public void onComplete() {
System.out.println(tag + "=====onComplete=====");
}
};
}
private Observable<Integer> getCompleteObservable() {
return Observable
.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter)
throws Exception {
emitter.onNext(4);
emitter.onNext(5);
emitter.onNext(6);
emitter.onComplete();
}
});
}
private Observable<Integer> getErrorObservable() {
return Observable
.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter)
throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onError(new Error("-error-"));
}
});
}