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. 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.