AAkira / Napier

Logging library for Kotlin Multiplatform
Apache License 2.0
786 stars 34 forks source link

Add native coroutines example #72

Closed AAkira closed 3 years ago

AAkira commented 3 years ago

closes #56, #69

Run background thread

You can use this library on a background thread on iOS using Kotlin.coroutines as native-mt.

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
}
backgroundScope.launch {
    suspendFunction()
}