https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

dharrigan

08/11/2019, 12:15 PM
Which of these two approaches would be better? (and are they idomatic?)
m

Marko Mitic

08/11/2019, 1:09 PM
Depends on what you need. #1 could have better performance if that's critical, otherwise #2 looks better
If you'd need to wait on writes to be completed to do some other work, you could just wrap single launch into a
coroutineScope {  }
and make
doSomething
suspending
Had a brain fart here,
coroutineScope {  }
would be more useful for #1
Also, try avoiding GlobalScope
I usually write it like this
Copy code
suspend fun doSomething() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
    writeIntoTable1()
    writeIntoTable2()
    writeIntoTable3()
}
g

gildor

08/11/2019, 2:31 PM
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
is not needed if
writeIntoTableN
is suspend function
3 Views