Difference between doAfterTerminate and doFinally

后端 未结 1 1895
有刺的猬
有刺的猬 2021-01-01 10:15

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

相关标签:
1条回答
  • 2021-01-01 11:04

    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.

    0 讨论(0)
提交回复
热议问题