在协程中切换到 UI 上下文

Switching to UI context in coroutines

我是协程的新手,我想知道是否可以将下面代码的协程范围 (GlobalScope) 切换到 UI 范围。我的问题是coroutine launch body里面的步骤必须在工作线程中执行,否则监听器通知必须在ui线程中执行以避免在我的activity代码中调用runOnUiThread。

override suspend fun startRent(name: String, bikeMode: BikeMode, listener: StartRentListener) {
        var bleDevice : RxBleDevice
        val scanFilter: ScanFilter = ScanFilter.Builder().setDeviceName(name).build()
        val scanSettings: ScanSettings = ScanSettings.Builder().build()

        val job = GlobalScope.launch {
            try {
                bleDevice = rxBleClient.scanBleDevicesExt(rxBleClient, scanSettings, scanFilter)
                val bleConnection = bleDevice.establishConnectionExt()
                // write handshake
                connectionManager.writeHandshake(bleDevice, bleConnection)
                // open lock
                openLock(bleDevice, bikeMode, bleConnection)
                // getting user position
                apiHelper.sendLockRequest(bleDevice.name, getPosition())
                bleDevice.disconnect()
                // this should be called on main thread once all the previous operations are finished
                listener.onSuccess()
            } catch (e: Exception) {
                listener.onError(e)
            }
        }
        job.join()
    }

我当前的 activity 代码片段:

bikeAccessClient.startRent(bikeBLEName, BikeMode.HYBRID, object :
                    StartRentListener {
                    override fun onSuccess() {
                        runOnUiThread {
                            // UI update here
                        }
                    }

您可以使用 withContext(Dispatchers.Main) {..} 函数与其他 Coroutine Dispatcher 一起执行您的部分代码。

kotlinx.coroutines.android 包含 Dispatchers.Main 函数的定义,它与 Android UI.

正确集成

在您的代码中使用显式 Dispatcher 非常容易出错。相反,我会建议设计具有较少明确要求的代码。

我会写这样的东西:

fun uiActionHandlerToStartTheProcess() {
  launch(Dispatchers.Main) {
     val result = startRent(...) // no callback here, suspend function
     
     //UI Update Here
   }
}
suspend fun CoroutineScope.startRent() : SomeResultOfWork {
   //that function offloads the execution to a IO (aka brackground) thread
   return withContext(Dispatchers.IO){
     //here goes your code from `startRent`
     //use `suspendCancellableCoroutine {cont -> .. }` if you need to handle callbacks from it
    
     SomeResultOfWork()
   } 

launch(Dispatchers.Main){..}块中的代码在UI线程中执行。调用 startRent 挂起函数会挂起 UI 线程中的执行。一旦 startRent 准备好回复(来自后台线程),它将恢复执行(由 Dispatchers.Main 完成并等效于 runOnUiThread {...})并执行 UI 从正确的线程更新