New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flow: Operator for action on successful completion #1693
Comments
But your flow will never successfully complete. It does not stop emitting unless canceled
If it had finite number of emissions, you could simply write your code after last call to
You can also easily write your own operator to do this further downstream:
Or you can do this simply after final call to
In all cases, those actions will only be executed, if flow has been successfully collected (not canceled, not failed on exception). |
@xizzhu In addition to the above comment (you can easily write your own operator for that), I would really like to know why you need that. What is your use-case? Why would you need an operator for successful (non-cancelled) completion? |
Thanks @circusmagnus for the suggestion! @elizarov I have a flow that downloads from a server, and emits the downloading progress. When the downloading finishes (not-cancelled), I want to show a toast to inform the user. Is the suggestion to put the "doAfterLastEmit" action directly after |
Flow.onCompletion now reports all failures and cancellation in its cause just like invokeOnCompletion. A null cause is reported if and only if flow had completed successfully (no failure, no cancellation). Emission of additional elements after the end of the flow is only possible from inside of onCompletion block in case of successful completion. Also fixed a bug where onCompletion implementation was trying to add exception to its own list of suppressed exceptions, which is not allowed. Fixes #1693
Flow.onCompletion now reports all failures and cancellation in its cause just like invokeOnCompletion. A null cause is reported if and only if flow had completed successfully (no failure, no cancellation). Emission of additional elements after the end of the flow is only possible from inside of onCompletion block in case of successful completion. Also fixed a bug where onCompletion implementation was trying to add exception to its own list of suppressed exceptions, which is not allowed. Fixes #1693
Flow.onCompletion now reports all failures and cancellation in its cause just like invokeOnCompletion. A null cause is reported if and only if flow had completed successfully (no failure, no cancellation). Emission of additional elements after the end of the flow is only possible from inside of onCompletion block in case of successful completion. Also fixed a bug where onCompletion implementation was trying to add exception to its own list of suppressed exceptions, which is not allowed. Fixes #1693
…otlin#1732) Flow.onCompletion now reports all failures and cancellation in its cause just like invokeOnCompletion. A null cause is reported if and only if flow had completed successfully (no failure, no cancellation). Emission of additional elements after the end of the flow is only possible from inside of onCompletion block in case of successful completion. Also fixed a bug where onCompletion implementation was trying to add exception to its own list of suppressed exceptions, which is not allowed. Fixes Kotlin#1693
According to the doc, the
onCompletion()
"operator is transparent to exceptions that occur in downstream flow and does not observe exceptions that are thrown to cancel the flow".If I want to do something only when the flow is "successfully completed" (i.e. no exception, not cancelled), how can I do that? Thanks.
It can be reproduced with the following code:
The text was updated successfully, but these errors were encountered: