https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Maciek

07/23/2020, 11:21 AM
Is there a way to share emitted value between consumers of the flow that's backed by the channel? Can't figure out a way for both consumers to get the same value. In attached test producer is playing ping pong between them. I hope SharedFlow will be released soon, but until it is, is there any other way? 😅
d

Desmond Teo

07/23/2020, 11:30 AM
I think BroadcastChannel is what you’re looking for
m

Maciek

07/23/2020, 11:33 AM
it's working, thanks!
If I'm using openSubscription like that, should I somehow close it after flow is closed?
m

molikuner

07/23/2020, 11:38 AM
I think you should be able to use asFlow instead of this function. Then you don’t have the problem of closing.
👍 1
m

Maciek

07/23/2020, 11:43 AM
but now
BroadcastChannel
broke that producer produces only when there're subscribers, this is another requirement. Is there a way to have both, so shared value and a lazy producer?
m

molikuner

07/23/2020, 11:55 AM
I’m not sure whether that fixes your problem, but you could try to use send instead of offer.
m

Maciek

07/23/2020, 12:08 PM
unfortunetly it doesn't because BroadcastChannel is buffered or confloated so it won't suspend the call straight away
7 Views