Retrofit + RxAndroid: slow request block others requests

1.1k views Asked by At

I am facing a problem which is due to slow replies from my backend sometimes. In fact, I am using Retrofit + RxAndroid to communicate with my server and when I am doing multiple requests, it seems that only one request is executed at a time. Is there any way to increase this limit to avoid blocking waiting requests ?

Thank you.

edit:

Imagine that I am doing one request to long background process.

retrofitService.getLongBackgroundProcess(url)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { //doing something with the result });

And at the same time I am doing another request to a service that can respond immediately.

retrofitService.getVeryVeryFastResponse(url)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { //doing something with the result });

The first request in my case is blocking the execution of the second request.. This is my problem. Do you know why and what can i do to having both requests executed at the same time ?

Thanks.

2

There are 2 answers

0
eVoxmusic On BEST ANSWER

It seems that the problem was from neither retrofit nor rxandroid. My backend seems to be the problem.

The backend took too long to send back the response from the request, so I passed my long processing jobs to the background and then replied directly to the users. This is more efficient and doesn't block the available threads on the Android client.

Thank you for your help.

3
Sergii Pechenizkyi On

You can control where requests are executed by yourself like this:

retrofitService.getImage(url)
.subscribeOn(Schedulers.io()) // or .subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bitmap -> myImageView.setImageBitmap(bitmap));

There are numbers of schedulers available. Check the documentation here: http://reactivex.io/documentation/scheduler.html.

Good read on topic: http://blog.danlew.net/2014/10/08/grokking-rxjava-part-4/