我有以下方法请求从端点获取口袋妖怪。
我想通过快速单击将多次调用此方法的按钮来防止用户提出快速请求。我使用了 throttle * 方法和去抖动。
基本上,如果用户在 300 毫秒内快速单击按钮,我正在寻找它应该接受该持续时间内的最后一次单击。但是,我正在经历的是所有请求都在发出。即,如果用户在该持续时间内快速点击 3 次,我仍然会收到 3 个请求。
fun getPokemonDetailByName(name: String) {
pokemonDetailInteractor.getPokemonDetailByName(name)
.subscribeOn(pokemonSchedulers.background())
.observeOn(pokemonSchedulers.ui())
.toObservable()
.throttleFirst(300, TimeUnit.MILLISECONDS)
.singleOrError()
.subscribeBy(
onSuccess = { pokemon ->
pokemonDetailLiveData.value = pokemon
},
onError = {
Timber.e(TAG, it.localizedMessage)
}
).addTo(compositeDisposable)
}
最佳答案
Bascially, what I am looking for if the user rapidly clicks on the button within 300 milliseconds duration it should accept the last click in that duration
对我来说听起来更像是去抖运算符(operator)的行为。从文档
Debounce — only emit an item from an Observable if a particular timespan has passed without it emitting another item
你可以看到大理石图here
关于android - 防止快速单击按钮并使用 rxjava 发出请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62814430/