【发布时间】:2019-07-07 09:11:15
【问题描述】:
我必须使用对房间数据库的异步调用来更新 UI,但是当我这样做时,我遇到了这个错误:android.view.ViewRootImpl$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程可以触摸其观点。
//FavoritesPresenter.kt
GlobalScope.launch {
favoritesView.showFavorites(ProductProvider.getAllProducts() as ArrayList<Product>)
}
// ProductProvider.kt
fun getAllProducts() : MutableList<Product> {
return dao.getAllProducts()
}
// ProductDao.kt
@Query("SELECT * FROM product")
fun getAllProducts(): MutableList<Product>
我需要通过我的 ProductProvider 更新我的 UI,因为我将用于我需要可靠解决方案的所有实体。
【问题讨论】:
标签: android kotlin android-room kotlin-coroutines coroutine