Extension function instead of redefining uiScope everywhere

This commit is contained in:
arkon
2020-04-17 20:06:33 -04:00
parent 088160ed32
commit d9f44c1f7d
17 changed files with 57 additions and 59 deletions

View File

@@ -6,8 +6,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
fun <T> Flow<T>.launchInUI(): Job = CoroutineScope(Dispatchers.Main).launch {
collect() // tail-call
}
fun launchUI(block: suspend CoroutineScope.() -> Unit): Job =
GlobalScope.launch(Dispatchers.Main, CoroutineStart.DEFAULT, block)