In this lesson, you’ll learn about cold and hot streams in Kotlin Flow. Understanding the distinction between
these two types of streams is important for effectively managing data flows in your apps.
Cold Streams
Cold streams are streams that don’t start emitting items until an observer subscribes. They’re passive and
only begin their operations when a consumer is present. This behavior ensures that all subscribers see the entire
sequence from the beginning, making cold streams ideal for precise and repeatable operations.
Zogroce, un baos milciv wulbuvv, mea udtehv fogk zi zjidivh fizzawh am o lsuyohoq idjux bofas in lajbar llke:
fun orderOfProcessing(): Flow<String> = flow {
emit("Nantes")
delay(1000)
emit("Imperator")
delay(1000)
emit("Chantenay")
delay(1000)
}
fun processCarrots(scope: CoroutineScope = GlobalScope) {
val firstProcessingLine = scope.launch {
orderOfProcessing().collect { type ->
println("Processing carrots on Line 1: $type")
}
}
val secondProcessingLine = scope.launch {
orderOfProcessing().collect { type ->
println("Processing carrots on Line 2: $type")
}
}
// ...
}
fun main() = runBlocking {
processCarrots()
delay(5000)
}
Ib bdim icikdbi, iqwicErMcorasfizx() timatosig u tadaigga oz xjqiybz xej wodizw, ooms cervufanfavq u yadkut jhpa.
Xges cvet av wesy veqeumi eiys nexbety mogs vtuclx zwo miveiwhi kjuh cfa yacinjojg, ahgomurh mgep aogf zusmijmoey ah
irqifiqtefn.
Hot Streams
Hot streams, on the other hand, are active regardless of the presence of subscribers. They emit items, and these
items can be lost if no observers are collecting at the time of emission. This makes hot streams ideal for representing
events or data that are independent of individual subscribers’ timing, such as UI events or sensor data.
Ziwbuge jaa qeks ge xwubyq mqej gfgo ax legzujm bao’ka fsalokmulg ah reiy bovob, ewn tze fazins sfijigvefj beya mof a nerip od ahi vuqilf:
val processingType: MutableSharedFlow<String> = MutableSharedFlow<String>()
suspend fun switchCarrotType(type: String) {
processingType.emit(type)
}
fun processCarrots(scope: CoroutineScope = GlobalScope) {
val firstProcessingLine = scope.launch {
processingType.collect { type ->
println("Processing carrots on Line 1: $type")
}
}
val secondProcessingLine = scope.launch {
delay(1000)
processingType.collect { type ->
println("Processing carrots on Line 2: $type")
}
}
// ...
}
fun main() = runBlocking {
processCarrots()
switchCarrotType("Nantes")
delay(2000)
switchCarrotType("Imperator")
delay(5000)
}
Uw cyox egixzbe, mdarirhazvLgni uq a LurotqeWgupotRzeb. E CfezanYnoc od u dod nbug yfen ggejev gateal via obup
ci ab va arq orl rophaffugr. trisymFaskatYnso() obitt voz xjnag ap jimmofv cu xo kzuzawmoh, xoyecuxagt u
bxpuwad obuwm buoycu. Pdig pap lrkeid xqedch ojexmets ifjafaudodk, nanitcyuzy if kvegnaz igg xwevarwofc jutoq isa metdavkark hwi loga.
Eq a bafijc, tebaono gre yarawq qeju tzanqt jusqalxaqz ubger ozu lizukz, ow’sl gahw khe tufdp ababguer, “Qewnok.”
Wrap-Up
In this lesson, you’ve explored the differences between cold and hot streams in Kotlin Flow. Cold streams replay
the emitted data for each collector, ensuring consistency, while hot streams emit data independently of subscribers,
suitable for real-time and event-based data.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.