I am trying to understand why we need callbackFlow builder, it seems almost same with channelFlow except callbackFlow is inline. What is the use case ?
callbackFlow common. fun <T> callbackFlow(block: suspend ProducerScope<T>.() -> Unit): Flow<T> Content copied to clipboard. Creates an instance of a coldFlow with elements that are sent to a SendChannel provided to the builder's block of code via ProducerScope.
The main difference between flows and channels is this: Flows are *usually* cold and channels are hot. In other words, when using a flow the data is produced within the stream while in channels the data is produced outside of the stream.
A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages.
They do exactly the same thing. One of them literally calls the other. The difference is in the intention. It is supposed to make your code more self documenting about your intentions.
Use callback flow for callbacks and channelFlow for concurrent flow emission.
EDIT:
As of Version 1.3.4, callbackFlow
will detect missing calls to awaitClose
, making it less error prone.
So they are now different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With