J. K. J. K. - 3 months ago 9
Android Question

RxView.debounce waits for the period of the debounce to execute the command, how do I execute the command right away?

I have the following code in my Android app, trying to prevent multiple clicks of a button:

RxView.clicks(bSubmit)
.debounce(2500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(c -> displayToast());


but instead of executing the code, and then preventing multiple clicks in the same timespan from being acted on,
what this code does is, it executes the command after the debounce timespan has passed.

How can I achieve what I want?

Answer

According Reactivex.io documentation, debounce emit the last event during a time window.

What you want is to emit the first event during a time window, which what does throttleFirst (see documentation).

RxView.clicks(bSubmit)
        .thottleFirst(2500, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(c -> displayToast());
Comments