For the More Curious: SharedFlow

In this chapter, you saw two ways of creating a flow: using the flow builder and the MutableStateFlow type. These two approaches cover the vast majority of use cases, but for special cases there is another class called MutableSharedFlow (and its corresponding read-only type, SharedFlow).

MutableSharedFlow is a more generic version of MutableStateFlow. In fact, MutableStateFlow extends from MutableSharedFlow. So, what is it good for?

Suppose you want to collect from a flow with two collectors. The first collector will be added right after you create the flow, but the second collector will not be added until some time after. If you used the flow function to build the flow, you would find that both ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.