RxJava - two observables

Asked by At

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. I've tried .zip, but for some reason it does not get triggered:

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?

1 Answers

1
Sarath Kn On Best Solutions

zip will emit the items to the downstream only after both of your observables (OnPeriodChanged, OnFilterChanged) emitted. I think you are trying to call HistoryViewModel.getScans whenever any of the item changes, with latest values of Date and Filter. You could use combineLatest instead of zip

Try changing it to

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