Does anybody knows what is the difference between operators "doAfterTerminate" and "doFinally" in RxJava 2 ?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With