Reputation: 277
How Can I prevent multiple long api calls (e.g. user tap the button several times) without saving state (e.g. saving state to "isLoading" property).
Upvotes: 2
Views: 5307
Reputation: 1693
You can use the debounce function for this:
.debounce(400, TimeUnit.MILLISECONDS)
Upvotes: 0
Reputation: 10267
There are missing requirements here, but assuming you want to avoid making additional calls while there's one executing, until request is finish, you can use take(1)
with repeat()
and optionally also retry()
, take(1)
will limit emission to the first click, repeat()
will resubscribe again when onComplete()
- which ,means the network request done, so you will able to receive single click again and perform request. you can consider also retry() for resubscribing with failure (that will not repeat the request, but will make request available again when clicked)
getClicksEvents()
.take(1)
.flatMap(aVoid -> getRequestObservable())
.repeat()
.retry()
.subscribe( result -> //do your thing );
Upvotes: 4