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

Arkadii Ivanov

02/03/2020, 10:27 PM
BroadcastChannel
drops items if there are no subscribers. Is there a way to not drop but buffer? Looking for something with
UnicastSubject
behaviour.
g

Gabriel Feo

02/03/2020, 10:33 PM
I think you just need to pass a positive
capacity
value to the BroadcastChannel factory function https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-broadcast-channel.html
a

Arkadii Ivanov

02/03/2020, 10:36 PM
Nope, this is stated in docs: "Note: this channel looses all items that are send to it until the first subscriber appears;"
g

Gabriel Feo

02/03/2020, 10:40 PM
I can’t find that, do you remember where? AFAIK, BroadcastChannel doesn’t impose that limitation
a

Arkadii Ivanov

02/03/2020, 10:43 PM
Just open the link you provided 🙂
message has been deleted
😅 1
g

Gabriel Feo

02/03/2020, 11:22 PM
Sounds like you need a cold stream, actually. How about a buffered Flow?
a

Arkadii Ivanov

02/03/2020, 11:33 PM
From my point of view Flow will not buffer anything unless subscribed. Isn't it?
I’m not sure
g

gildor

02/04/2020, 2:00 AM
Yes, Flow is cold, will not buffer before start subscribing on it. You can just open subscription on this channel and do not consume elements, so it will start buffering
a

Arkadii Ivanov

02/04/2020, 9:05 AM
Thanks, will give it a try.
3 Views