Introduction to Kotlin Flows and Channels


That is a part of the asynchronous circulate sequence:

Kotlin Circulation is chilly stream, Kotlin Channel is sizzling stream. The variations between cold and warm streams are summarized under.

Chilly Stream vs Sizzling Stream

Behaviors Chilly Stream Sizzling Stream
The place knowledge is produced? Contained in the stream outdoors the stream
Unicast or multicast? Unicast Multicast
Lazy or keen stream? Lazy stream Keen stream

Terminology

  • Unicast – has just one subscriber per stream, new subscription creates new stream
  • Multicast – has no or many subscribers per stream, new subscription makes use of current stream
  • Lazy stream – begin emitting values solely when somebody begin subscribing to it
  • Keen stream – begin emitting values even it does NOT have any subscribers

Kotlin Flows

Kotlin_Flows.drawio.png

Circulation knowledge is produced contained in the stream by creating the Circulation and calling the FlowCollector<T>.emit(). It’s known as upstream circulate.

val circulate: Circulation<Int> = circulate {
    repeat(10000) { worth ->
        delay(1000)
        emit(worth)
    }
}

Nonetheless, the information will not be emitted till the Circulation<T>.accumulate() known as. That is an instance of gathering circulate in ViewModel. It’s known as downstream circulate.

viewModelScope.launch {  
    circulate.accumulate {  worth ->
        
    } 
}

Every time the Circulation<T>accumulate() known as, a brand new chilly stream is created. It’s known as unicast. Information is simply emitting at this level – lazy stream.

Kotlin Channels

Kotlin_Channels.drawio.png

Channel knowledge is produced outdoors the stream. Creating the channel and sending the information into the recent stream are separated. Sending knowledge to the recent stream known as channel’s sender.

Create the Channel

personal val channel = Channel<Int>()

Ship Information into the Sizzling Stream

viewModelScope.launch {
    repeat(10000) { worth ->
        delay(1000)
        channel.ship(worth)
    }
}

Sizzling stream is sort of a buffer which has the buffer capability. The default buffer capability is zero. By default, when the buffer is overflow, the information sending is suspended till there’s a receiver.

To obtain the channel’s knowledge, you name the Channel.obtain(). That is known as the channel’s receiver.

viewModelScope.launch {
    whereas(true) {
        val worth = channel.obtain()
    }
}
  • Because the buffer capability is zero by default, the buffer overflow occurs as quickly as you are sending the primary knowledge into the stream. Thus, the information sending is suspended after the primary knowledge till there’s a receiver. This conduct seems to be similar to lazy stream, however it’s NOT. It’s nonetheless an keen stream.

  • Think about if the buffer capability is greater than zero, sending knowledge happens even with none receiver till the buffer is full. Thus, it’s an keen stream.

In contrast to Circulation, having a brand new subcriber or new channel’s receiver doesn’t create a brand new stream. It nonetheless makes use of the present stream. Thus, it’s multicast stream.

One factor I want to emphasize is as soon as the information is obtained, the information is faraway from the recent stream / buffer.

Kotlin_Channels_2.drawio.png

Primarily based on the diagram above, assuming there are already channel’s receiver #1, #2 and #3 subscribing to the channel (calling the Channel.obtain()operate), the channel’s sender is sending 1, 2 and 3 to the recent stream.

  • As soon as 1 is obtained by receiver #1, receiver #2 and #3 will not obtain 1 anymore.
  • Receiver #2 receives 2 as an alternative, and receiver #3 receives 3.

Conclusion

I simply summarize the conduct of utilizing Circulation and Channel at high-level. I do not likely know any sensible usages of them, particularly Channel.

These are the notes that I’ve written down:

  • Circulation – retailer single worth that adjustments over time
  • Channel – retailer one-time occasion, collected by one observer?

Circulation most likely is sensible. I’ve been utilizing it to reveal the information from a neighborhood database. However actually, when will we use Channel? Snackbar? When you have an excellent sensible utilization of it, please share that with me.

[Updated – Sept 23, 2022]: After performing some researches and located the next article by Google group which mainly does NOT advocate Channel for one-time occasion. Briefly, use all the pieces with StateFlow – will probably be coated in subsequent article.

Supply Code

GitHub Repository: Demo_AsyncFlow (see FlowActivity and ChannelActivity)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles