You can only use subscribeOn() once for each stream. If you use it a second time, it will not do anything. Thus, when you combine your two methods together, you run:
observeOn(AndroidSchedulers.mainThread())
which switches the operation to the main thread. After that, it stays there because the next subscribeOn() actually ignored.
I would suggest that you are actually too complex with your layout method. Just add
subscribeOn(Schedulers.io())
For your network calls, then use
observeOn(AndroidSchedulers.mainThread())
Before you want to process the results in the main thread. You will get something like:
public Observable<String> networkCall1() { return <NETWORK_CALL_1()> .subscribeOn(Schedulers.io); } public Observable<String> networkCall2( String input ) { return <NETWORK_CALL_2(input)> .subscribeOn(Schedulers.io); } public Observable<String> chainedCalls() { return networkCall1() .flatMap( result1 -> networkCall2( result1 ) ) .observeOn(AndroidSchedulers.mainThread()); }
EDIT
If you really want to have a call to observeOn() for specific group call methods. You need to add extra observeOn() to your chainedCalls() method. You can have as many observeOn() calls as you want for each thread. It will be something like:
public Observable<String> networkCall1() { return <NETWORK_CALL_1()> .subscribeOn(Schedulers.io) .observeOn(AndroidSchedulers.mainThread()); } public Observable<String> networkCall2( String input ) { return <NETWORK_CALL_2(input)> .subscribeOn(Schedulers.io) .observeOn(AndroidSchedulers.mainThread()); } public Observable<String> chainedCalls() { return networkCall1() .observeOn(Schedulers.io) .flatMap( result1 -> networkCall2( result1 ) ) .observeOn(AndroidSchedulers.mainThread()); }
source share