-1

I'm working with Rxjava2,

I'm using flatmap in RxJava with below structure:

Observable1.flatmap() and return to Observable 2.

as below codes :

getApi().createUser(os, deviceToken)
            .compose(view.regisObserver())
            .subscribeOn(Schedulers.io())
            .flatMap(result -> {
                String user_token = result.data.user_token;
                getPreferenceStore().setAuthToken(user_token);

                setReadPolicy();

                Observable<ObjectDto<UserProfile>> obs = getApi().updateProfile(null, null, null);
                        obs.compose(view.regisObserver());

                return obs;
            })
            .subscribe(result-> {
                getPreferenceStore().setUserId(result.data.user_id);

            //                    view.onUpdateProfile();
            }, Throwable::printStackTrace);

@Override
public <T> ObservableTransformer<T, T> regisObserver() {
    return observable -> observable.compose(prepare())
            .doOnSubscribe(disposable -> showProgressDialog())
            .doOnComplete(this::closeProgressDialog)
            .doOnError(throwable -> {
                if (BuildConfig.DEBUG) {
                    throwable.printStackTrace();
                }
                showProgressDialog();
                closeProgressDialog();
            });

}

The code compiles without error. It got error in run-time NetworkErrorOnMainThread. I don't know how to fix it.

Huy Tower
  • 7,769
  • 16
  • 61
  • 86
  • You should not run any network calls on MainThread, Use a separate thread. – Sujith Niraikulathan Feb 23 '17 at 09:54
  • 1
    Cmon, it has been answered so many times... http://stackoverflow.com/questions/34349334/rxjava-and-retrofit2-networkonmainthreadexception http://stackoverflow.com/questions/27687907/android-os-networkonmainthreadexception-using-rxjava-on-android http://stackoverflow.com/questions/33390532/networkonmainthreadexception-with-retrofit-beta2-and-rxjava http://stackoverflow.com/questions/23447077/android-rxjava-non-blocking – Than Feb 23 '17 at 10:04

1 Answers1

6

You need to specify that you want to subscribe on non-UI thread...for example

.subscribeOn(Schedulers.io())

Actually, looks like you are doing that for updateProfile....just need to also do it for createUser

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63