2

I have following sample that demontrates the problem:

Observable<Integer> observable = Observable.create(subscriber -> {
    IntStream.range(0, 1_00).forEach(subscriber::onNext);
    subscriber.onCompleted();
}).doOnCompleted(() -> System.out.println("I'm first, but want to be second"));

observable.subscribe((i)-> {}, (e)-> System.err.println(e), ()-> System.out.println("I'm second, but want to be first"));

If I replace observable doOnCompleted with doAfterTerminate it works, but it has slightly different semantics.

How to achieve that subscriber onCompleted will be called before doOnCompleted on that observable ?

marknorkin
  • 3,904
  • 10
  • 46
  • 82

1 Answers1

0

You should use doFinally() This is the side effect method which will be called after onCompleted.

You can also refere this : https://stackoverflow.com/a/44922182/7409774

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
  • As I understood from docs this is the deprecated method in favour of doAfterTerminate and so has same semantics(will be called even if error occurred), which is note desired behavior http://reactivex.io/documentation/operators/do.html – marknorkin Jul 15 '17 at 09:30