Joshua Hansen
12/14/2023, 4:48 PMtransformResultRow()
is an expensive operation.
Option 1:
suspend fun transformData(): List<MyData> {
val results: List<ResultRow> = queryForData() // queryForData() is a suspend fun querying the db
return withContext(Dispatchers.Default) {
results.map { resultRow ->
async {
transformResultRow(resultRow) // fun transformResultRow(row: ResultRow): MyData
}
}.awaitAll()
}
}
Option 2: (What's the difference? They both seem to be doing the same thing)
suspend fun transformData(): List<MyData> {
val results: List<ResultRow> = queryForData() // queryForData() is a suspend fun querying the db
return coroutineScope {
results.map { resultRow ->
async(Dispatchers.Default) {
transformResultRow(resultRow) // fun transformResultRow(row: ResultRow): MyData
}
}.awaitAll()
}
}
Joshua Hansen
12/14/2023, 4:56 PMJoshua Hansen
12/14/2023, 4:58 PMcoroutineScope
is preferred here conventionally.Daniel Pitts
12/14/2023, 4:58 PMuli
12/19/2023, 4:04 PM