java – Observable runs on main thread even though subscribeOn() is called on another thread-ThrowExceptions

Exception or error:

I got a weird issue in one of my activities.
When coming back from taking a picture / video, in my onActivityResult I am showing a dialog that lets the user name the camera.
Once the user presses OK, I send onNext() to a subject with the requested file name that copies the file (and shows progress dialog).

For some reason the map() function that does the copy is always called on the main thread, even though I call subscribeOn(

protected void onActivityResult(final int requestCode, int resultCode, Intent intent) {

    final PublishSubject<String> subject = PublishSubject.create();`

    mSubscription = subject
            .map(new Func1<String, String>() {
                public String call(String fileName) {
                    return doSomeIOHeavyFuncition();
            .subscribe(new Action1<String>() {
                public void call(final String fullPath) {

            }, new Action1<Throwable>() {
                public void call(Throwable throwable) {
            }, new Action0() {
                public void call() {

    final AlertDialog dialog = new AlertDialog.Builder

            .setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    String someString = getStringFromDialog(dialog);

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);


Changing the subscribeOn( call to observeOn( solved the issue.
Still I would like to know why it didn’t work…

How to solve:

subscribeOn and observeOn is the mostly confused operators there are. The former makes sure that subscription side effects happen on the specified scheduler (thread), but that doesn’t mean that values will pop up on that thread as well.

For example, if your Observer opens a network connection when one subscribes to it, you don’t want that to run on the main thread, therefore, you need subscribeOn to specify where that subscription and thus the network connection will be created.

When data finally arrives, the emitting thread can be anything, one of the schedulers or a background plain old thread. Since we don’t know or don’t like that thread, we want to move the observation of the data to another thread. This is what observeOn does: makes sure operators after it will execute their onNext logic on the specified scheduler. Android devs use it already to move the observation of values back to the main thread.

What’s rarely explained though is what happens when you want some extra computation off the main thread before the final result lands on the main thread again: use multiple observeOn operators:

.map(v -> heavyCalculation(v))
.doOnNext(v -> { saveToDB(v); })

Leave a Reply

Your email address will not be published. Required fields are marked *