Could you please to help me resolve this. I’m looking for a flow can adapt the requirement: Every s...
b
Could you please to help me resolve this. I’m looking for a flow can adapt the requirement: Every single should be collect only one time, the value will be exist until it has been consumed and can be apply the filter. I’m have try with channel, but it have no function to filter value but keep it in the queue value if not match the conditional For example:
Copy code
val numberChannel = Channel<Int>(capacity = Channel.BUFFERED)
GlobalScope.launch {
    numberChannel.send(0)
    numberChannel.send(1)
    numberChannel.send(2)
    numberChannel.send(3)
    numberChannel.send(4)
    numberChannel.send(5)
    numberChannel.send(6)
    numberChannel.send(7)
}

GlobalScope.launch {
    delay(1000)
    numberChannel.receiveAsFlow()
        .filter { it % 2 == 0 }
        .collectLatest {
            Timber.d("------Collect1: $it") //expect print: 0,2,4,6
        }
}

GlobalScope.launch {
    delay(2000)
    numberChannel.receiveAsFlow()
        .collectLatest {
            Timber.d("------Collect2: $it")  //expect print: 1,3,5,7
        }
}
🚫 1
e
channels and flows are sequentially ordered, you cannot skip items.
filter
will always consume, whether it passes it on or drops it.
👍 1
b
Thanks for your time. I understand that channel just consume the item without any filter or conditional on receive.
Anyway, how can I can adapt the requirement ?