Skip to content

Instantly share code, notes, and snippets.

@liberaid2
Last active November 5, 2019 15:03
Show Gist options
  • Save liberaid2/ea6035a5211ddcbf804fcc9fc0d94482 to your computer and use it in GitHub Desktop.
Save liberaid2/ea6035a5211ddcbf804fcc9fc0d94482 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit) = this.launch(Dispatchers.Main, block = block)
suspend fun <T> CoroutineScope.withUI(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Main, block = block)
fun CoroutineScope.launchCatching(block: suspend CoroutineScope.() -> Unit, onError: (ctx: CoroutineContext, throwable: Throwable) -> Unit) = launch(CoroutineExceptionHandler(onError), block = block)
fun CoroutineScope.launchUICatching(block: suspend CoroutineScope.() -> Unit, onError: (ctx: CoroutineContext, throwable: Throwable) -> Unit) = launch(CoroutineExceptionHandler(onError) + Dispatchers.Main, block = block)
// include this:
/* KotlinX coroutines */
def coroutines_version = '1.3.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutines_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment