Home > Back-end >  What is the hot flow and cold flow in coroutines and the difference between them?
What is the hot flow and cold flow in coroutines and the difference between them?

Time:09-23

I want to mastering kotlin coroutines and trying to figure out

1- what is hot flow and cold flow ?

2- what is the main difference between them?

3- when to use each one?

CodePudding user response:

A cold stream does not start producing values until one starts to collect them. A hot stream on the other hand starts producing values immediately.

I would recommend to read below to understand hot and cold steams with usage:

https://developer.android.com/kotlin/flow/stateflow-and-sharedflow

CodePudding user response:

Form Here ( )

Cold vs Hot streams

Well, I really struggled with this concept because it is a little bit tricky. The main difference between cold and hot happened to be pretty simple: Hot streams produce when you don’t care while in cold streams, if you don’t collect() (or RxJava-s equivalent subscribe()) the stream won’t be activated at all. So, Flows are what we call cold streams. Removing the subscriber will not produce data at all, making the Flows one of the most sophisticated asynchronous stream API ever (in the JVM world).

  • Related