Hello everyone! The question is, why doesn’t the c...
# coroutines
r
Hello everyone! The question is, why doesn’t the channel make an “offer” for every “emit” from the list flow?
Copy code
val channel = ConflatedBroadcastChannel<String>()
launch {
            listOf("1", "2", "3", "4", "5", "6").asFlow()
                .onEach { Log.d("ORIGIN FLOW(onEach()):", it) }
                .collect {
                    channel.offer("Channel: $it")
                }

            channel.asFlow()
                .onEach {
                    Log.d("TEST FLOW(onEach()):", it)
                }
                .flatMapMerge { merge(flow1(), flow2()) }
                .collect {
                    Log.d("TEST FLOW(collect()):", it)
                }

        }
b
collect
is suspend fun, so you need to launch second coroutine for consuming channel.asFlow()
otherwise firstly you will consume the whole 1,2,3,4,5,6 flow, and since channel is conflated it will contain only the latest value when you start collecting it.
r
Thank you for the answer, splitting it into several coroutines has solved the problem, but now even with the
buffer
operator the channel will have time to receive only the first and the last element, is there any way that the whole chain will run on each item?
b
try to use
BroadcastChannel
instead of
ConflatedBroadcastChannel
.
ConflatedBroadcastChannel
broadcasts only latest element.
👍 1