Sobes.tech
Junior — Middle

What is the type of data stream that is used by default in Flow?

sobes.tech AI

Answer from AI

By default, Kotlin Flow uses a type of data stream called a Cold Stream. This means data is generated and transmitted only when someone subscribes to the stream. Until there are subscribers, no computations or emissions occur.

Example:

val flow = flow {
    println("Flow started")
    emit(1)
    emit(2)
}

fun main() = runBlocking {
    println("Before collect")
    flow.collect { value -> println(value) }
    println("After collect")
}

In this example, the message "Flow started" will appear only when collect is called, i.e., upon subscription.