Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between doAfterTerminate and doFinally

Tags:

java

rx-java2

Does anybody knows what is the difference between operators "doAfterTerminate" and "doFinally" in RxJava 2 ?

like image 917
Andrey Turkovsky Avatar asked Nov 15 '17 11:11

Andrey Turkovsky


People also ask

What is doFinally?

The difference is that doFinally executes the provided Action if the downstream cancels/disposes the sequence in addition to the regular onError or onComplete termination paths. This allows cleaning up and releasing resources by all three means.

What is single Rxjava?

Single. Single is an Observable that always emit only one value or throws an error. A typical use case of Single observable would be when we make a network call in Android and receive a response. Sample Implementation: The below code always emits a Single user object.


1 Answers

The difference is that doFinally executes the provided Action if the downstream cancels/disposes the sequence in addition to the regular onError or onComplete termination paths. This allows cleaning up and releasing resources by all three means. The operator also guarantees that the action gets executed exactly once per subscription even in case if the onError or onComplete signals race with a cancellation.

In contrast, doAfterTerminate only covers onError and onComplete.

You can emulate doFinally with doAfterTerminate + doOnCancel, however, being split a operation, the action parameters may be both executed and cause problems with non-idempotent cleanup logic.

like image 195
akarnokd Avatar answered Sep 26 '22 03:09

akarnokd