ar-g
05/06/2020, 1:37 PMfun getAll(): Flow<...> { //works in parallel, doesn't respect structural concurrency
GlobalScope.launch {
//start request...
}
return db.flow()//starts emitting immediately...
}
suspend fun getAll(): Flow<...> { //works sequentially
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
launch {
//start request...
}
}
return db.flow()//start emitting after request is finished...
}
Dominaezzz
05/06/2020, 3:03 PMar-g
05/06/2020, 3:35 PMdb.flow()
starts emitting only after request finished.Dominaezzz
05/06/2020, 3:38 PMfun getAll(): Flow<...> = flow { //works sequentially
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
//start request...
}
emitAll(db.flow())//start emitting after request is finished...
}
ar-g
05/06/2020, 3:42 PMar-g
05/06/2020, 3:43 PMDominaezzz
05/06/2020, 3:44 PMfun getAll(): Flow<...> = flow { //works in parallel, but only once flow is collected
coroutineScope {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
//start request...
}
emitAll(db.flow()) //starts emitting immediately...
}
}
Dominaezzz
05/06/2020, 3:47 PMwithContext
waits for the launch
to finish because Structured Concurrency.ar-g
05/06/2020, 3:51 PMcourutineScope
function. I expected that everything which is launched with different Dispatcher would be executed in parallel.araqnid
05/06/2020, 4:34 PM