Skip to content Skip to sidebar Skip to footer

Progress Dialog Not Showing Up Before Rxjava Call

I have the following code, where I start a ring dialog before I make a long running task, done in RxJava2. The problem is that the dialog isn't showing up and I don't think I'm bl

Solution 1:

How is createDocument implemented? create, fromCallable

@akarnokd I do the calculations then do a Single.just(fileNameAndContacts)

As suspected, you compute the document on the current thread (main) and blocking it. You should move it into fromCallable and it will compute in the background when combined with subscribeOn(Schedulers.io()):

Observable.fromCallable(() -> {
     /* compute the document here */return fileNameAndContacts;
}); 

Solution 2:

I suggest doing RxAndroid2 in this way

// Init your dialogProgressDialogringProgressDialog=newProgressDialog(this);
ringProgressDialog.setTitle("Your Title");
ringProgressDialog.setMessage("Your message");

createDocument(filenameAndContacts)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnSubscribe(disposable -> ringProgressDialog.show())
        .doOnTerminate(() -> {
            if (ringProgressDialog.isShowing()) {
                ringProgressDialog.dismiss();
            }
        })
        .concatMap(fileAndContacts -> sendDocumentInEmail(fileAndContacts))
        .subscribe(this::onSuccess, this::onError)


privatevoidonSuccess() {
    // Do anything you want on Android main thread
}

privatevoidonError() {
    // Timber..
}

Explanation

The method doOnSubscribe() will be called whenever your observable start to subscribe and doOnTerminate() will be called just before the resulting Observable terminates, whether normally or with an error.

Then, if you want to do another thing like sendDocumentInEmail after you get fileAndContacts, just use concatMap()

So, whenever error happens, you can handle it in onError() method.

Doing like this will help you avoid callback hell or side-effect, which easily lead you to error prone.

Reference:

Hope this help

Post a Comment for "Progress Dialog Not Showing Up Before Rxjava Call"