android - 在主线程中访问协程时出现非法状态异常
问题描述
我已经编写了下面给出的代码来从本地数据库中获取数据并将其更新到我的回收站视图。我Dispatchers.IO
用于从数据库中获取数据并Dispatchers.main
用于在回收站视图中更新我的列表。但我无法访问调度程序主模块。我在我的片段中调用以下函数:
viewLifecycleOwner.lifecycleScope.launch(Main) {
eventList = withContext(IO) {
shruthiDatabase.eventDao().getAllEvent() as ArrayList<Event_Entity>
}
if (eventList.isNotEmpty()) {
eventAdapter!!.notifyDataSetChanged()
eventRecycler.visibility = View.VISIBLE
eventFound.visibility = View.GONE
}
}
解决方案
您的依赖项中可能存在问题。有时,当版本不匹配时,主调度程序不起作用
COROUTINES_VERSION = "1.3.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$COROUTINES_VERSION"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$COROUTINES_VERSION"
推荐阅读
- regex - 向现有正则表达式添加另一个正则表达式
- docker - 用于构建和部署捆绑包的 docker 卷
- java - Android 布局 - 禁用不必要的区域
- php - 如何在 Woocommerce 结帐页面中添加额外费用复选框
- python - string.split() 的意外行为
- gcc - 为什么 GCC 在“westmere”架构上默认禁用以下选项,尽管“westmere”支持它们
- reactjs - 带有 shopify 北极星 Reactjs 的 Google 字体
- r - 将 ifelse 与日期时间数据一起使用时,如何停止隐式日期转换?
- c++ - vkEnumerateDeviceExtensionProperties 抛出神秘的错误代码
- c# - Xamarin.Forms:Map.IsShowingUser 不显示当前位置?