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

Andrew Gazelka

06/02/2019, 5:33 AM
is there something like a channel but which can have two receivers?
g

gildor

06/02/2019, 5:41 AM
Yes, BroadcastChannel
a

Andrew Gazelka

06/02/2019, 5:42 AM
ok also kinda confused on when to use channels vs flows... think I should use a channel for events which go on indefinitely?
or at least turn a flow into a broadcast channel?
with
Flow#broadcastIn(...)
g

gildor

06/02/2019, 5:43 AM
Flow is cold stream, it doesnt have multiple receirs by definition
But you can open Flow to BroadcadtChannel
broadcastChannel.asFlow()
Turning flow to broadcast channel possible too, yes, what is your use case
Flow is cold stream, Channel is hot
a

Andrew Gazelka

06/02/2019, 5:44 AM
well I have something like this which polls email subjects... iyo is it better for this to be a Flow or a Channel?
g

gildor

06/02/2019, 5:44 AM
Also, I would recommend to ask in #coroutines
a

Andrew Gazelka

06/02/2019, 5:44 AM
aight
g

gildor

06/02/2019, 5:46 AM
In this case I would use Flow
It's lazy
a

Andrew Gazelka

06/02/2019, 5:47 AM
isn't this basically "hot" data though? Or technically cold because produced with a function
g

gildor

06/02/2019, 5:47 AM
And for cases when I want to share it between multiple receirs (request email once and broadcast to all receivers), ai would convert it to BroadcadtChannel
a

Andrew Gazelka

06/02/2019, 5:47 AM
usually I see hot data as events and whatnot which this kinda is I guess
aight
g

gildor

06/02/2019, 5:47 AM
As I said, depends on case, but I don't see why this case should be hot in the first place
But for some use cases it's fine to convert it to hot (broadcast, or even conflated broadcast)
a

Andrew Gazelka

06/02/2019, 5:49 AM
I mean cold data you need to go through all data right? and this might be bad since you are getting data indefinitely
maybe I am not understanding hot vs cold properly
g

gildor

06/02/2019, 6:50 AM
Not necessary all data, you can close flow when you want. like you can dispose RxaJava observable, the same way you can cancel coroutine where you consuming Flow, and good thing about flow in this case that it will start only when someone consumes it (your channel above will be started eagerly, and eagerly compute first value and continue invocation until this channel will not be closed, for example in case of iterator usage on channel it will not be closed) and will be finished when consumer will be cancelled
👍 1
And you can use cold streams for infinite streams of data with no problems, but this stream will be started when someone consumes it and finished when consumer will be stopped, and without some hot adapter cannot be consumed twice or by multiple receivers simultaneously