SrSouza
05/06/2020, 4:24 PMreceiveAsFlow().filter {}.collect()
but I think that this is not the right aproach be cause do not work. How I can await the first appearance of a value in a Channel?Joffrey
05/06/2020, 4:32 PMchannel.consumeAsFlow().first { ... }
If other consumers need to use your channel as well, and want to see all values (including the one that this statement would return), then you’ll need to use a broadcast channel instead (to send the same value to multiple consumers). Once you have a broadcast channel, you can do:
broadcastChannel.asFlow().first { ... }
Using receiveAsFlow()
is kind of tricky because it will fan out the values between all consumers (a given value would only reach one consumer), so you can’t be sure that the one who needs this first value matching the predicate will get it.SrSouza
05/06/2020, 4:42 PMbroadcastChannel.asFlow().first {}
is the one.Joffrey
05/06/2020, 4:43 PMasFlow()
and share it between all your consumers. Each collection or other terminal operators (like first()
) on this flow creates a new subscription to your broadcast channel, and cancels it in the end.SrSouza
05/06/2020, 4:45 PMBroadcastChannel.openSubscription
I could use consumeAsFlow
without any problem too?Joffrey
05/06/2020, 4:46 PMconsumeAsFlow()
basically auto-cancels the channel (in this case the subscription channel) after the first consumption of the flow.