dharrigan
08/11/2019, 12:14 PMmarstran
08/11/2019, 12:17 PMdharrigan
08/11/2019, 12:18 PMdharrigan
08/11/2019, 12:18 PMmarstran
08/11/2019, 12:18 PMdharrigan
08/11/2019, 12:19 PMsuspend fun writeIntoTable1() = GlobalScope.launch {...}
dharrigan
08/11/2019, 12:19 PMmarstran
08/11/2019, 12:20 PMGlobalScope
. Your coroutines could leak.dharrigan
08/11/2019, 12:20 PMdharrigan
08/11/2019, 12:20 PMmarstran
08/11/2019, 12:21 PMfun CoroutineScope.doSomething() {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
writeIntoTable1()
}
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
writeIntoTable2()
}
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
writeIntoTable3()
}
}
dharrigan
08/11/2019, 12:22 PMmarstran
08/11/2019, 12:22 PMCoroutineScope
as the receiver parameter is a convention.uli
08/11/2019, 12:22 PMsuspend
keyword bacause you are not suspending, but returning imediately: fun CoroutineScope.writeIntoTable1Async() = launch {...}
dharrigan
08/11/2019, 12:23 PMmarstran
08/11/2019, 12:23 PMmyScope.doSomething()
.marstran
08/11/2019, 12:24 PMdharrigan
08/11/2019, 12:24 PMdharrigan
08/11/2019, 12:24 PMdharrigan
08/11/2019, 12:47 PMuli
08/11/2019, 12:48 PMgildor
08/11/2019, 2:34 PMgildor
08/11/2019, 2:36 PMsuspend fun doSomething() {
coroutineScope { // or withContext(IO) if you really need IO disaptcher
launch {
writeIntoTable1()
}
launch {
writeIntoTable2()
}
launch {
writeIntoTable3()
}
}
}
And on call site:
myLocalScope.launch { doSomething }
or any other semantics that suits better call site (use await, just call suspend function etc)dharrigan
08/11/2019, 3:40 PMdharrigan
08/11/2019, 3:40 PM