So I always have analysis paralysis when trying to...
# coroutines
c
So I always have analysis paralysis when trying to figure out which Flow subtype to use. I use StateFlow mostly, but I have an example today of having to essentially display a bunch of logs from user events onto the screen (as a debug machanism). So essentially I'm building a log viewer. A "flow" makes sense for me to use here, but I can't use the
flow {}
builder because I need to be able to emit from anywhere in my class. Is "SharedFlow" essentially the way to go here, or what?
👌 1
K 1
z
Lots are hot streams of events, so yea SharedFlow is probably what you’d want. StateFlow is for state, not events, and flow {} is for cold streams (generally).
c
"Logs"*, right?
👍🏻 1
oooh. i like that mental cheat sheet
Copy code
hot streams of event = sharedFlow
cold streams = flow
state = stateFlow
I think maybe the name of sharedFlow confuses me? why choose "shared" as a prefix there for that type of flow?
z
Because multiple collectors “share” what it produced - they each get every emission that occurs while they are subscribed
Contrast to a channel, where every emission is only given to a single consumer
p
StateFlow will conflate events
u
If your requirement is mainly bein able to send from multiple threads, there is also channelFlow
c
Because multiple collectors “share” what it produced - they each get every emission that occurs while they are subscribed
💡 dont know why i didn't connect the dots... lol. thanks!!!
Contrast to a channel, where every emission is only given to a single consumer
ive never used channels 🙈 ... but from the name they would seem to be "hot" and allow multiple subscribers... basically sharedFlow. because by nature a TV channel is producing the video stream. even if no one is watching.
z
I don’t think there’s any useful comparison to be made with tv channels 😂
These are channels from CSP, like go’s
They’re very different from flows in many ways, but in general they’re closer to a blocking queue. No matter how many consumers are waiting for a value on a channel, when a value is sent (enqueued), only one consumer will receive (dequeue) it.
c
> I don’t think there’s any useful comparison to be made with tv channels oh. lol. gotta look up csp. noted about being more like a blocking queue. thanks. like i said. i never used it so i guess i haven't been missing anything.