如何在 MainActivity 中管理协程并等待完成?

chr*_*isu 1 android android-activity kotlin kotlin-coroutines coroutinescope

我使用GlobalScopewith runBlockingin MainActivity,但我不使用那里的流程,只是挂起功能。我想GlobalScope从协程更改为其他范围。

用例

class UpdateNotificationListItemUseCase @Inject constructor(private val notificationDao: NotificationDao): BaseUpdateBooleanUseCase<Int, Boolean, Boolean, Boolean, Unit>() {
    override suspend fun create(itemId: Int, isRead: Boolean, isArchived: Boolean, isAccepted: Boolean){
        notificationDao.updateBooleans(itemId, isRead, isArchived, isAccepted)
    }
}
Run Code Online (Sandbox Code Playgroud)

主要活动

val job = GlobalScope.launch { vm.getIdWithUpdate() }
runBlocking {
    job.join()
}
Run Code Online (Sandbox Code Playgroud)

主视图模型

suspend fun getIdWithUpdate() {
     var id = ""
     id = notificationAppSessionStorage.getString(
            notificationAppSessionStorage.getIncomingKeyValueStorage(),
            ""
        )
        if (id != "") {
           
            updateNotificationListItemUseCase.build(id.toInt(), true, false, false)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的建议:

我已阅读文档https://developer.android.com/kotlin/coroutines/coroutines-best-practices

val IODispatcher: CoroutineDispatcher = Dispatchers.IO
val externalScope: CoroutineScope = CoroutineScope(IODispatcher)
      suspend {
         externalScope.launch(IODispatcher) {
              vm.getIdWithUpdate()
         }.join()
      }
Run Code Online (Sandbox Code Playgroud)

第二种选择,但在这里我不等到工作完成

suspend {
     withContext(Dispatchers.IO) {
          vm.getIdWithUpdate()
     }
}
Run Code Online (Sandbox Code Playgroud)

你怎么看待这件事?不是提供给ANR吗,我也阻塞线程。

Ser*_*gey 6

您可以使用lifecycleScopeinMainActivity代替 来GlobalScope启动协程:

lifecycleScope.launch { 
    vm.getIdWithUpdate() // calling suspend function
    // here suspend function `vm.getIdWithUpdate()` finished execution
    // ... do something after suspend function is done
}
Run Code Online (Sandbox Code Playgroud)

要使用lifecycleScope添加依赖项:

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:$2.4.0'
        
Run Code Online (Sandbox Code Playgroud)

GlobalScope强烈建议不要使用。并且不需要调用job.join(),调用后可以在协程构建器块中做一些事情vm.getIdWithUpdate(),例如更新UI。该协程正在使用Dispatchers.Main上下文运行。