How to debounce a retrofit reactive request in java?

后端 未结 1 1897
无人共我
无人共我 2021-01-11 14:26

I\'m working on an android project that makes requests through retrofit using Rx-Java observable and subscribe.

However, in some interactions this request can be cal

相关标签:
1条回答
  • 2021-01-11 14:59

    Suppose you want to start an execution according to some trigger event.

    Observable<Event> trigger = ... // e.g. button clicks
    

    You can transform the trigger events to calls to your API like this:

    trigger
        .debounce(1, TimeUnit.SECONDS)
        .flatMap(event -> mApi.getOnlineUsers())
        .subscribe(users -> showThemSomewhere(users));
    

    Also, notice that the debounce operator will take the last occurrence within the time frame, but throttlefirst will take the first. You may want to use one or the other depending on your use case.

    0 讨论(0)
提交回复
热议问题