Does this equivalence table make sense?
# coroutines
t
Does this equivalence table make sense?
s
For many use-cases, this makes sense. However, don’t count out `Channel`s entirely 🙂
Flow
‘handles’ back-pressure, by not having any 🙂 When calling
emit
in a flow, you can only do that on the scope of the collector, you can’t switch scope, e.g. you can’t do
launch { emit(data) }
. If you need to be able to do this, you’d need to create a
channelFlow
or
callbackFlow
, which introduce a Channel that has back-pressure
🚫 2
t
Interesting @streetsofboston. Thank you! 🙂
v
Flow
‘handles’ back-pressure, by not having any 🙂
Suspension is a back-pressure in the same manner as
Channel()
(with default zero capacity). Flow effectively handles back-pressure, and if you need more than one in-flight element,
buffer(n)
can be used
4
👍 3
s
Ah… I didn’t consider suspension to be back-pressure; it just suspends. But, yes, it does effectively acts as one with zero-capacity. @Vsevolod Tolstopyatov [JB] I found one thing with a flow without a buffer (just suspending). When running the
flow.collect
on the
Dispatchers.Main
and the flow itself on
<http://Dispatchers.IO|Dispatchers.IO>
, by using
flowOn
, the flow effectively got a buffer size > 0…
t
Still about the table… I should use
Flow
for cold observables and `Channel`s for hot observables, right?
Or I could achieve both behaviours using only
Flow
?
s
You have to call
collect
to make a flow ‘active’.
When you call
collect
the lambda you provided to
flow
,
channelFlow
, etc, is called. The
collect
suspends until that lambda has finished. The flow’s lifecycle and the collector’s lifecycle are tied together like this. I consider a
Flow
to be a special kind of
suspend fun
. Whereas a
suspend fun
only returns one result in a non-blocking way, a
Flow
can ‘return’ a stream of values in a non-blocking way. 🙂