android - 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
中不可用,将其作为参数处理不是一个好习惯。
你们有什么建议?
解决方案
我建议使用:
CoroutineScope(Dispatchers.IO).launch {}
推荐阅读
- r - R根据数据框/小标题中的突出计算值生成滞后值
- c++ - 关于jsoncpp的内存管理问题
- python - 评估多个机器学习模型的正确方法
- r - 放大 R - plotly 使用 dynamicTicks
- c++ - 对于 constexpr 函数中的 constexpr
- java - 为什么我的随机字符串生成器不能始终生成 100,000 个字符串?
- python - Python3.7 将对象写入文件
- java - 寻找骑士所有独特的可能动作
- r - 用于字符向量时的 match vscharmatch
- javascript - Browserify 和 Babelify 给出 {classname} 未定义错误