RxJava:将Observable与忽略发射中的一个结合起来

时间:2016-01-03 16:50:40

标签: android rx-java

我有可观察的Observable<Integer> observable1PublishSubject<String> observable2(参见示例)。

我的目的:

  1. Observables并行执行;
  2. 在一个观察者中处理错误;
  3. 不关心observable1将在onNext上发出的内容,仅对处理onError的错误很重要,并保证执行observable。
  4. 我需要像

    这样的东西
    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
                }
            };
    

1 个答案:

答案 0 :(得分:2)