XYOracleNetwork/sdk-ble-android

View on GitHub
ble-android-sample/src/main/kotlin/network/xyo/ble/sample/UIThread.kt

Summary

Maintainability
A
1 hr
Test Coverage
package network.xyo.ble.sample

import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

class UiCoroutineScope : CoroutineScope {

    private var parentJob = Job()

    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + parentJob

    fun onStart() {
        parentJob = Job()
    }

    fun onStop() {
        parentJob.cancel()
        // You can also cancel the whole scope with `cancel(cause: CancellationException)`
    }
}

val ui = UiCoroutineScope()