2

I need to show a progress dialog when I subscribe to a Completable and hide it after the operation is completed(successfully or with an error) or is canceled.

So I do

final Completable completable = notificationRepository.markAllAsRead()
        .doOnSubscribe(d -> progressDialog.show())
        .doOnError(error -> progressDialog.dismiss())
        .doOnComplete(() -> progressDialog.dismiss())
        .doOnDispose(() -> progressDialog.dismiss());

Is there any elegant way to get single callback when onError, onComplete or onDispose happens?

Rostyslav Roshak
  • 3,859
  • 2
  • 35
  • 56

1 Answers1

20

I have done some tests, so

doOnDispose is called when the subscriber cancels the subscription, it is never called after completion.

doOnComplete is called when the Observable/Completable completes successfully. (Error doesn't happen)

doOnError is called when Observable/Completable emits an error. (OnComplete is not called).

doOnTerminate is called when Observable/Completable emits an error or completes. So it's doOnComplete + doOnError

doFinally is called when Completable/Observable signals onError or onComplete or gets disposed. So it is doOnDispose + doOnError +doOnComplete.

So doFinally is what I was looking for, Thank you akarnokd for help.

Benjamin Mesing
  • 4,075
  • 1
  • 18
  • 22
Rostyslav Roshak
  • 3,859
  • 2
  • 35
  • 56