【问题标题】:RxJava - Emit when the third source changeRxJava - 当第三个源更改时发出
【发布时间】:2019-04-17 04:55:37
【问题描述】:

其实我也有这样的东西

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

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

但是我需要添加另一个 observable 源,因为我需要来自另一个 observable 的更多信息。使用 combineLatest 和 3 个源,trackPage() 被调用了两次..

是否有类似“仅在第三个可观察到的变化时发出”之类的运算符? 或类似的运算符可以让我从 3 个来源进行跟踪,并仅在可见性变化时发出新的跟踪。

谢谢!

【问题讨论】:

  • 您是否尝试过我在回答中的建议 :) ?

标签: observable rx-java rx-java2


【解决方案1】:

我不完全确定我是否正确理解了您的场景,但也许您可以使用 zipWithcombineLatest 与新的 observable 链接起来。 zipWith 只会在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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多