- Update Kotlin 1.5.21
- Run on other threads 🎉
Thanks @PhilipDukhov
Example
You can use this library on a background thread on iOS using Kotlin.coroutines as native-mt.
- Define coroutine scopes
internal val mainScope = SharedScope(Dispatchers.Main)
internal val backgroundScope = SharedScope(Dispatchers.Default)
internal class SharedScope(private val context: CoroutineContext) : CoroutineScope {
private val job = Job()
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
println("[Coroutine Exception] $throwable")
}
override val coroutineContext: CoroutineContext
get() = context + job + exceptionHandler
}
- Usage
backgroundScope.launch {
suspendFunction()
}