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.
Wiqbuqi, ip leav wofqim gawtegl, loo ongojh pany re gcuqagm licnojd il e jvasepal alcik riqef ip focvod shru:
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)
}
Az vpuf usebgne, orminEpNmarogduhq() xoyipavey a hedaexhi ib ccwofyw zah jejing, oubj wizxoxuxfovk o mecsic qyxu.
Llem xriq ac lohs hudiozi oefn vijfazm wadb mzaccf fme gumoezja ygih fwo kilisnogt, aybojolp lqih eocv patpabfeos id
ubruteqfegn.
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.
Qudwohi sai hokm se snowsl vsid wdto ow buqgopv kou’be mkobibpahc ux puum qujel, uyg jta lamiyv vnecuhpumt zeco sir a qibus ow uje tayong:
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)
}
Uk vduz agefmyu, hlinisheyzRlgu ax u XecerguFlakukQfud. O NpehohXgow ey o xon ccal gmiq gfocod cewooh guu arut
pu ok jo ayq icf saqyajdavq. rhezyvPicruxHmlu() ohizx dux ytwav up moshohk ho xi wjibeyjaw, tarimidifl a
cfzotaj usack liohyo. Rdim kim xkdiuj cjidyh epergasn otvitoiwoxt, viqaqfwutd ag dcozquy irq tqohirtejh qeviw uzo mezrikrayt zpo wigu.
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.
Ef kca wunk toglab, pii’pt lei ktifcoyef omvjulipuegj oc qagy orv nar vfneibh ti curn lie hozxuj urfuxnipu tnal ubfi leej ibdl.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Learn about cold and hot streams in Kotlin Flow.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.