Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using subscribeOn with Retrofit

There is conflicting information about when and whether to use subscribeOn with Retrofit.

Here is an answer saying to not use subscribeOn.
Here is an answer seeming to imply that subscribeOn has no good default set.
Here is example code using subscribeOn.

So, once for for all, when should I use subscribeOn and with what thread? What are the possible ramifications of using or not using subscribeOn?

apiService.issueRequest()
    // Is this useful? Required? Bad practice?
    .subscribeOn(Schedulers.io())
    // Do actions on main thread
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Response>() {
        @Override public void call(Response response) {
            handleResponse(response);
    });
like image 886
William Avatar asked Feb 11 '15 19:02

William


1 Answers

In the current version of Retrofit (1.9.0), Retrofit use his own executor to perform the http call and don't use the executor backed by the schedulers given by the subscribeOn method.

In your case, the scheduler will be used only to execute the code that will add your http call to the executor used by retrofit. (So it's a bit useless...)

BUT, regarding the actual code from Retrofit on Github, retrofit stop to use his executor, so it may be possible to used a RxJava scheduler instead.

like image 107
dwursteisen Avatar answered Oct 03 '22 13:10

dwursteisen