Remy Benza
03/22/2021, 9:28 AMSupervisorJob
?val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
scope.launch {
flow1.collect { //code }
flow2.collect { //code }
}
Natsuki(开元米粉实力代购)
03/22/2021, 9:36 AMSupervisorJob
, that's becoz in semantic flow is cold, thus when runs flows2.collect
that indicate flow1.collect
is done, use launchIn
or fire another launch
with each collectKamilH
03/22/2021, 9:47 AMcollect
is a suspend function so it suspends until flow1
finishes. It’s always better to use onEach
+ launchIn
combinationRemy Benza
03/22/2021, 9:48 AMKamilH
03/22/2021, 9:51 AMflow1.onEach {
// handle new value here
}.launchIn(scope)
scope.launch {
flow1.collect {
// handle new value here
}
}
flow1.onEach {
}.launchIn(viewModelScope)
flow2.onEach {
}.launchIn(viewModelScope)
without waiting for flow1 to finish, while here:
viewModelScope.launch {
flow1.collect {
}
flow2.collect {
}
}
flow2.collect
will be called only after flow1
finishedRemy Benza
03/22/2021, 9:59 AMKamilH
03/22/2021, 10:03 AMJob
case whole job would be cancelled while in SupervisorJob
it wouldn’tdewildte
03/22/2021, 1:41 PMSupervisorJob
then the scope will not be canceled..collect
does not start a new coroutine, it just suspends the current coroutine until the collection is complete.Remy Benza
03/22/2021, 3:00 PM