Retrofit+rxjava unique request in stack

541 views Asked by At

The problem: How to make retrofit+rxjava request unique? For example user can click on button a lot of, and the are will be a lot of requests. So, my point here, how to send only one request, but other, same requests, drop automatically. (There are should only one same request running at time)

Solution with booleans working(checking before you execute the task, that this task are already running), but maybe someone can suggest better, more universal solution?

Thank a lot of.

2

There are 2 answers

0
akarnokd On

Sounds like you could use distinct() or distinct(Func1). For example, given an input box and a button, you want to take the input box value and do some network calls if the user clicks on the button:

TextBox text = ...
Button button = ...

button.clickObservable()
.map(e -> text.getText())
.distinct()
.flatMap(txt -> networkApi.someCall(txt))
.observeOn(uiScheduler)
.subscribe(...)
0
Stepango On

Here is a solution I use in current project.

Each request - class with custom logic. Each request has unique request id. Each request has access to HashMap that contains id and subscription class. Before start new request i checked if map already contains an id key and if is so there is two variants(depends on request specific) i could unsubscribe previous request and launch new or just ignore starting of new request.