Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between channelFlow and callbackFlow

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 ?

like image 545
toffor Avatar asked Dec 05 '19 05:12

toffor


People also ask

What is callbackFlow?

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.

What is Channel Flow Kotlin?

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.

What is a kotlin Coroutine?

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.


1 Answers

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.

like image 109
Dominic Fischer Avatar answered Sep 19 '22 12:09

Dominic Fischer