Summary
This chapter focused on Kotlin Flow cancelations. You learned that Flows follow the cooperative cancellation of coroutines. The flow{}
builder and StateFlow
and SharedFlow
implementations are cancellable by default. You can use the cancellable
operator to make other Flows cancellable.
We then learned about retrying tasks with Kotlin Flow. You can use the retry
and retryWhen
functions to retry the Flow based on the number of attempts and the exception encountered by the Flow.
Then, we learned about handling exceptions that can happen during the emission or collection of data in a Flow. You can use the try-catch
block or the catch
Flow operator to handle Flow exceptions.
We learned how to handle Flow completion. With the onStart
and onCompletion
operators, you can listen and run code when Flows start and when they have finished. You can also emit values with the onStart
and onCompletion
code blocks, such as when you want to set an initial and final value for the Flow...