1

I've got a chain of Observables and a dialog that is dismissing after everything is finished.The order is this: 1 api call get ResponseBody 2 take response body process (not ui thread) 3 other process (not ui thread)

During the first call the dialog is okay, when it comes to the second and I receive the body of the first call the dialog is blocked and it remain as is for the rest of the time.

At the end after everything is done, but I receive a warning says that "The app is doing to much work on the main thread".

I'm not doing anything on the main thread, so I don't really understand how i can unblock the dialog and keep everything on a separate thread.

showLoadingDialog();

        mZappAppApis.downloadDatabase(Token.getToken(AppConfig.TOKEN_SYNC_DOWNLOAD_DATABASE))
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .concatMap(new Func1<ResponseBody, Observable<String>>() {
                    @Override
                    public Observable<String> call(ResponseBody responseBody) {
                        return mDatabaseFileHelper.writeDatabaseToFile(responseBody);
                    }
                })
                .concatMap(new Func1<String, Observable<String>>() {
                    @Override
                    public Observable<String> call(String s) {
                        return mDatabaseFileHelper.copyDatabaseIntoZappApp();
                    }
                })
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        dismissLoadingDialog();

                        saveLocalTimestamp(timestamp);

                        flowContinueInterface.onContinueFlow();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e(e, "Error during processing new database");

                        dismissLoadingDialog();

                        flowContinueInterface.onStopFlow();
                    }

                    @Override
                    public void onNext(String result) {
                        Logger.d(result);
                    }
                });
Bhargav
  • 8,118
  • 6
  • 40
  • 63
dvdciri
  • 441
  • 6
  • 19
  • From what I can see in your code,` dismissProgressDialog()` is called only on 2 occasions, `onCompleted()` of your observer and in `onError()` so, either of these methods of your observer is being called, and if you are think the progress is getting dismissed before all the processing is done, then that means `onError()` is called, so put a log in your `onError()` and look for the error – Bhargav Aug 14 '16 at 10:50

2 Answers2

2

The concatMap work is happening on the main thread. You need to move the observeOn call to just above the subscribe call.

I would also move significant processing out of the subscriber into doOnCompleted and doOnError calls that are also placed before the observeOn.

Dave Moten
  • 11,957
  • 2
  • 40
  • 47
  • I'm gonna accept this answer because it was the first one in terms of time and give me some advice as well. Thank you, it works – dvdciri Aug 14 '16 at 14:29
1

Move your .observeOn(AndroidSchedulers.mainThread()) above the subscribe(… call. Everything after your observeOn(… is executed on this thread. You can see this in by printing out the current thread you are on:

.subscribeOn(Schedulers.newThread())
.concatMap(new Func1<String, Observable<String>>() {
            @Override
            public Observable<String> call(final String string) {
                    Log.i("Before", Thread.currentThread().toString());
                    return Observable.just(string);
                }
            })
.observeOn(AndroidSchedulers.mainThread())
.concatMap(new Func1<String, Observable<String>>() {
          @Override
          public Observable<String> call(final String string) {
                    Log.i("After", Thread.currentThread().toString());
                    return Observable.just(string);
          }
})
...
Stephan
  • 15,704
  • 7
  • 48
  • 63