Learning Kotlin Programming

Comparing StateFlow, SharedFlow, and CallbackFlow

All these look similar, but how they differ from each other in their application

--

Image from Pixarbay, by Sasint.

A few weeks ago, I wrote about making a normal flow hotter, using callbackFlow. I was posted with a question, “How is callbackFlow differ from stateFlow and sharedFlow?"

Here, is my attempt to uncover their differences. But in a nutshell

StateFlow

Use this in the case where you always like to get the last emitted value whenever you start collecting the value. There’s no intention to stop collecting it.

SharedFlow

Use this in the case where you only like to get the newest emitted data and not the last emitted value, when you start collecting the value. There’s no intention to stop collecting it.

CallbackFlow

Use this in the case where you like to like to start a collection and stop it at some point…

--

--