Does `Flow.collect()` block the parent coroutine f...
# coroutines
t
Does
Flow.collect()
block the parent coroutine from executing the code under the flow until the flow is cancelled?
Copy code
viewModelScope{
    // code here get executed
    someFlow.collect()
    // code here never executes
}
👌 1
d
Yes, Flow terminal operators (like collect) suspend the calling coroutine while the flow is collected.
t
Ha.. okay thanks @diesieben07 and @Adam Powell
p
Exactly, I would use the term suspend rather than block. If you take a look into the collect() signature you will see is marked as suspend. You can launch a child coroutine and collect from there to avoid this behavior or use launchIn() combinator which do that for you..
👍 3
s
Note that the code under/after the
collect
is executed after the Flow ends (cancelled or ended/completed normally)
a
cancellation generally means that
collect
throws
CancellationException