简体   繁体   English

RxJava - 两个可观察对象

[英]RxJava - two observables

I've got two observables - OnPeriodChanged and OnFilterChanged, and trying to figure out how to call a function for view adapter when one of them changes.我有两个 observables - OnPeriodChanged 和 OnFilterChanged,并试图弄清楚如何在其中一个更改时为视图适配器调用函数。 I've tried .zip, but for some reason it does not get triggered:我试过 .zip,但由于某种原因它没有被触发:

Observable.zip(OnPeriodChanged, OnFilterChanged, (Date, Filter) -> HistoryViewModel.getScans(Date.first, Date.second, Filter)).subscribe(scans -> histAdapter.setScans(scans));

What I can use here to invoke the getter function and pass the results from it to setter?我可以在这里使用什么来调用 getter 函数并将结果从它传递给 setter?

zip will emit the items to the downstream only after both of your observables ( OnPeriodChanged , OnFilterChanged ) emitted.只有在您的两个可观察对象( OnPeriodChangedOnFilterChanged )发出后, zip才会将项目发送到下游。 I think you are trying to call HistoryViewModel.getScans whenever any of the item changes, with latest values of Date and Filter .我认为您正在尝试在任何项目更改时调用HistoryViewModel.getScans ,并使用DateFilter最新值。 You could use combineLatest instead of zip您可以使用combineLatest而不是zip

Try changing it to尝试将其更改为

    Observable.combineLatest(OnPeriodChanged, OnFilterChanged, (Date, Filter) -> HistoryViewModel.getScans(Date.first, Date.second, Filter))
            .subscribe(scans -> histAdapter.setScans(scans));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM