dsg dsg - 2 months ago 50
Java Question

Default Schedulers for rxjava on Android

I'm using Retrofit to return rxjava Observable's for my async network calls.

I find myself repeating the following invocation:


Seems like I'm always subscribing on the IO thread and observing on the Android main thread. This seems to be the best practice that all the resources I found advocate. Perhaps other than long-running computations, I don't quite understand when we would want to deviate from this pattern.

Is there a way to remove this boilerplate by defaulting the subscribeOn and observeOn threads?

Is this a use case for rxjava plugins? (I can't find many examples of their use.)

Can I set the default threads at the network boundary by messing with the retrofit executors?


For Observable responses, Retrofit currently sets the subscribeOn as the HTTP executor of the RestAdapter (either provided or the default). This was done to shim RxJava support into the existing behavior.

The plan for 2.0 is to provide the ability to set defaults for both subscribeOn and observeOn explicitly (whether it be both, only one, or neither).

A reason you wouldn't want always want observation on the main thread is if you needed to chain multiple API calls together, for example.