Hey guys, if I have something like in my applicati...
# coroutines
a
Hey guys, if I have something like in my application I am noticing that flow2 doesn't ever get to execute but flow 1 does however, if I change the order and put flow2 before flow1, both flows execute. Would anyone know why this could be the case? For context in my application flow1 represents a SQLDelight DB transaction where as flow2 represents a network call.
Copy code
viewModelScope.launch { 
    flow1.collect {}
    flow2.collect {}
}
👍 1
j
probably flow1 never ends so flow2 is not going to be collected. In another hand flow2 ends and when it ends, flow1 starts to collect
So you should use a launch per flow or use onEach + launchIn
☝️ 1
💯 1
c
.collect { }
is going to suspend until the flow completes, which may be never. Coroutines execute sequentially, so until
flow1
completes, the
launch { }
block will not be able to continue to
flow2
. The solution is to put each
.collect { }
in their own launch block, so that they will both be running in parallel
💯 1
a
Thank you both ! This was helpful ! I’ve been staring at the code for a while and it didn’t occur to me that flow1 might not be completing which makes sense since SqlDelight transactions as flow keep the channel open so that updates can be processed. Thank you again 🙂