androidrx-java2rx-androidreactivexrx-binding

don't re-execute request while user is clicking repeatedly on fetch data button using rxbinding and rxjava


I have a RxView and I have set the click observer, I don't wanna always make new request per click. The thing that I wanna is to ignore user clicks when the fetching data request is in progress only using Rxjava and Rxbinding.

button.clicks()
        .flatMapSingle {
            Completable.fromCallable {
                usersViewModel.users(emptyList())
            }
                .andThen(Request.users())
        }
        .doOnNext {
            Toast
                .makeText(this, R.string.users_updated, Toast.LENGTH_LONG)
                .show()
        }
        .subscribe(usersViewModel::users) { Log.e(this::class.java.name, it.message, it) }

Is there any way except the ThrottleFirst?


Solution

  • Disabling and reenabling the button is much more straightforward, but you can use backpressure:

    button.clicks()
            .toFlowable(BackpressureStrategy.DROP)
            .flatMapSingle ({
                Completable.fromCallable {
                    usersViewModel.users(emptyList())
                }
                .andThen(Request.users())
            }, false, 1)
            .doOnNext {
                Toast
                    .makeText(this, R.string.users_updated, Toast.LENGTH_LONG)
                    .show()
            }
            .subscribe(usersViewModel::users) { 
                Log.e(this::class.java.name, it.message, it) 
            }