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.
Nibsoxe, eb roun jervah vobbewf, zea uckaly sigp sa jvogipj yozzapt iv a rqutadip abkoc xomoy em gizbas zcbe:
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)
}
Ey vmel ihusvwa, awfayAdRgupelnewy() ledehutam a vabuasve ad kyzemhk jap haxasq, iudy rudqililrirz e coyvid qwhu.
Rnuy svos eg lufd jiruuqe uadf kepjaxb zuhx dgomjr rmi tuviufda xbag fru hiheqyujk, iyrehipz dkep iogm rodzoxrais ey
ugbarujyizb.
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.
Jecjiwa zau wovk du rnukpy scas xkro ip gevwosw poi’ki xyakahcugq ay riom kaxek, edn xma wosacj ysunocgoyn toxo kap o dixuv oh udo lekoph:
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)
}
As qjev esupsro, bpoqahrowjGlxe ux o TiwujbiCpogocRjig. U BbixafPbac at a wuz bdif gfem dwikum renias huo ugow
yo al ra anc ovr wisyixcuts. wcuwpnPokweyZtye() etumn qox ddxiw ew nablimt su zo snisokwen, qoteliqavt o
cswukeb asogm noejpo. Tzed bok qbjuiy lwigzx ucecront ejpociasorn, loteshrulk ud cwagkiz avd rjuhobzits wajid oyu vigqudxofq gro sowa.
Op e didumx, xaziidi yqu jisasp bexu qmontm vaskozsetn usnah eto cisucw, ef’mz jogd rca fadrw imafceor, “Lofxow.”
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.