我有可观察的Observable<Integer> observable1
和PublishSubject<String> observable2
(参见示例)。
我的目的:
observable1
将在onNext上发出的内容,仅对处理onError的错误很重要,并保证执行observable。我需要像
这样的东西observable2.mergeWith(observable1.map(i -> Integer.toString(i)))
.subscribe(mySubsriber);
但不从observable1
发出。
有很好的方法吗?
示例
long time1 = 7000;
long time2 = 1000;
Observable<Integer> observable1 = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
Handler handler = new Handler();
handler.postDelayed(() -> {
subscriber.onNext(1);
subscriber.onNext(2);
subscriber.onNext(3);
subscriber.onCompleted();
}, time1);
}
}).doOnNext(new Action1<Integer>() {
@Override
public void call(Integer data) {
cacheData(data);
}
});
PublishSubject<String> observable2 = PublishSubject.create();
Handler handler = new Handler();
handler.postDelayed(() -> {
observable2.onNext("A");
observable2.onNext("B");
observable2.onNext("C");
observable2.onCompleted();
}, time2);
Observer<String> observer = new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
//need handling errors from observable1 and observable2
}
@Override
public void onNext(String s) {
//need emitting only from observable2
}
};
答案 0 :(得分:2)
请参阅 ignoreElements 运算符http://reactivex.io/documentation/operators/ignoreelements.html。