https://kotlinlang.org logo
Title
t

tjohnn

12/22/2019, 3:56 PM
Does
Flow.collect()
block the parent coroutine from executing the code under the flow until the flow is cancelled?
viewModelScope{
    // code here get executed
    someFlow.collect()
    // code here never executes
}
:yes: 1
d

diesieben07

12/22/2019, 3:57 PM
Yes, Flow terminal operators (like collect) suspend the calling coroutine while the flow is collected.
t

tjohnn

12/22/2019, 3:59 PM
Ha.. okay thanks @diesieben07 and @Adam Powell
p

Pablichjenkov

12/22/2019, 5:12 PM
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

streetsofboston

12/22/2019, 10:40 PM
Note that the code under/after the
collect
is executed after the Flow ends (cancelled or ended/completed normally)
a

Adam Powell

12/22/2019, 10:49 PM
cancellation generally means that
collect
throws
CancellationException