Search code examples
androidandroid-livedatakotlin-flowkotlin-stateflowkotlin-sharedflow

Is LiveData hot or cold?


We know that StateFlow and SharedFlow are hot.

StateFlow is a hot flow—it remains in memory as long as the flow is collected or while any other references to it exist from a garbage collection root.

SharedFlow is a hot flow that emits values to all consumers that collect from it.

Flow by itself is cold.

Flows are cold streams similar to sequences...

I have a question that I couldn't find a direct answer to. Is LiveData hot or cold?


Solution

  • LiveData is not directly comparable to Flow. It's most directly comparable to StateFlow.

    Code that calls setData on a LiveData object are always going to change the state of the LiveData, regardless of any observers on it. In that respect, it's hot - the presence of observers doesn't matter at all. The state always changes, and that state is broadcast to any observers.

    Also, observers on a LiveData are always going to get a current state value out of it - they don't wait (or suspend) for any values to become available. LiveData must always have a value. Observers will get that value before any additional data is put in it.

    In these respects, LiveData is most likely considered "hot" based on its usage patterns that are similar to StateFlow.