Reputation: 1511
I am struggling with one RxJava use case and could use some assistant.
When a button is clicked I open a dialog to let user pick a value. Dialog needs initial value which is equal to previousely chosen value (or default one). I try to implement it like this:
Observables.combineLatest(
RxView.clicks(my_button),
viewModel.valueObservable
)
.subscribe { clickAndValuePair->
showDialog(
initialValue = clickAndValuePair.second
)
}
The problem is, when new value is picked the dialog is opened again, because new value is emitted in this combined Observable. What is the best practice to handle such situation?
Upvotes: 2
Views: 747
Reputation: 39873
You might be able to handle this with the zip()
operator:
Observables.zip(
RxView.clicks(my_button),
viewModel.valueObservable
).subscribe {
showDialog(initialValue = it.second)
}
The zip operation matches every n-th item of the observables together, thus it always waits for a click after emitting. For this you need to make sure to only update the value from the dialog and always update it from there. Even if you have not updated anything.
A better alternative might be to just get the latest value whenever there was a click with the withLatestFrom()
operator:
RxView.clicks(my_button)
.withLatestFrom(viewModel.valueObservable)
.subscribe { showDialog(initialValue = it.second)}
Upvotes: 3