所以,我在“onBindViewHolder”回收器的适配器方法中运行了这段代码:
launch(UI) {
val bitmapDrawable = loadLargeBitmapDrawable()
imageView.setImageDrawable(bitmapDrawable)
}
这使我的应用程序冻结了几秒钟,锁定了我的主线程。
但后来我改成这样:
launch { // <- I removed the "UI"
val bitmapDrawable = loadLargeBitmapDrawable()
launch(UI) { //Launch the UI coroutine inside the other
imageView.setImageDrawable(bitmapDrawable)
}
}
为什么会这样?协程的目的是在同一个线程(UI)中使事物异步,对吗?有人可以解释一下为什么我必须在另一个协程范围内运行 UI 协程?