RxJava-当第三个源更改时发出

时间:2019-04-16 19:56:54

标签: observable rx-java rx-java2

实际上我有类似的东西

Observable.combineLatest(presenter.getSomething1(), fragmentVisibility, Pair::create)
  .compose(bindToLifecycle())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(this::trackPage, this::error));

其中的getSomething1()提供了一些跟踪我的Fragment所需的信息,实际上它工作得很好。

但是我需要添加另一个可观察的来源,因为我需要来自另一个可观察的信息。有了CombineLatest和3个源,trackPage()被调用了两次。

是否存在像“仅在第三个可观察到的变化时才发射”这样的运算符?或类似的运算符,它使我可以从3个源进行跟踪并仅在可见性发生变化时发出新的跟踪。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的情况,但是也许您可以使用combineLatestzipWith与新的可观察物链接起来。每当zipWith发出值第三个observable发出值时,combineLatest只会发出一个值(应用所需的发射值的任何组合)。 (RxJava zip documentation)

Observable.combineLatest(presenter.getSomething1(), fragmentVisibility, Pair::create)
  .zipWith(thirdObservable(), (pairEmissionFromCombineLatest, emissionFromThirdObservable) {
    // Combine the emissions and emit a new value (Here I am just re-emitting the emitted value of the combineLatest observable
     return pairEmissionFromCombineLatest;
   })
  .compose(bindToLifecycle())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(this::trackPage, this::error));