首页 > 解决方案 > Recycler Adapter 中的协程

问题描述

对于房间分贝的目的。我想在 Recycler View 中运行一些协程。

必要的挂起函数作为类参数处理:

class RecyclerAdapter  (private val  exist : suspend (lastName : String) -> Boolean) 

然后,当需要时,我使用以下构造:

GlobalScope.launch(Dispatchers.IO) {
   if (exist(dataSet[position].lastName))
       [...]

我不确定使用 Global Scope 是否是最佳实践。我考虑使用lifecycleScope但在适配器lifecycleOwner中不可用,将其作为参数处理不是一个好习惯。

你们有什么建议?

标签: androidkotlinandroid-roomkotlin-coroutines

解决方案


我建议使用:

CoroutineScope(Dispatchers.IO).launch {}

推荐阅读