gpt4 book ai didi

android - 错误 : Cannot access database on the main thread since it may potentially lock the UI for a long period of time. - 使用 Kotlin 的 Android Room

转载 作者:行者123 更新时间:2023-12-02 12:16:08 25 4
gpt4 key购买 nike

好的,所以,我正在尝试使用 Android Room 为这个项目创建这个数据库。

首先,这是我在 Gradle 文件中关于该问题的内容:

//Room database
implementation "androidx.lifecycle:lifecycle-viewmodel:2.1.0"

implementation "androidx.room:room-runtime:2.2.3"
kapt "androidx.room:room-compiler:2.2.3"
implementation "androidx.room:room-ktx:2.2.3"

这是道代码:
@Dao
interface DebtsDao {

@Query("SELECT * FROM debts")
fun getDebtsList() : List<Debts>

@Query("SELECT * FROM debts WHERE name LIKE :name")
fun getNamedDebt(name : String) : Debts

@Insert
fun insertInDatabase(debt : Debts)

@Delete
fun deleteFromDatabase(debt : Debts)

}

数据库位:
@Database(entities = arrayOf(Debts::class), version = 1)
abstract class AppDatabase : RoomDatabase()
{
abstract fun debtsDao() : DebtsDao
}


这是我使用它的功能:
    fun refreshRecyclerView()
{
val database = Room.databaseBuilder(applicationContext, AppDatabase::class.java, "account").build()

val list : List<Debts> = database.debtsDao().getDebtsList()

recycler_view_debts.layoutManager = LinearLayoutManager(this)
recycler_view_debts.adapter = RecAdapter(this, list)
}

我尝试按照 Android 页面上的教程进行操作,但没有按预期工作。我在 Android Studio 上收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.watchingmoneyfly, PID: 17067
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.watchingmoneyfly/com.project.watchingmoneyfly.Activities.MainActivity}: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:351)
at com.project.watchingmoneyfly.RoomDatabase.DebtsDao_Impl.insertInDatabase(DebtsDao_Impl.java:79)
at com.project.watchingmoneyfly.Activities.MainActivity.onCreate(MainActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

这应该是问题的根源:无法访问主线程上的数据库,因为它可能会长时间锁定 UI。

我在这里想念什么?提前致谢!!

最佳答案

Room 阻止您从 UI 线程使用它,因为数据库查询将访问磁盘,这可能需要很长时间,并且在此期间阻塞 UI 线程会卡住用户界面。您应该从后台线程调用 Room,如何到达那里取决于您。您可以选择 Java 线程原语、执行程序、RxJava、协程等,具体取决于您熟悉的内容。

您可以使用 allowMainThreadQueries 在技术上规避 Room 的限制。 ,但您不应该在实际应用程序中这样做,原因如上所述。此开关仅用于测试。

您还可以考虑返回 LiveData , Flowable ,或与 Room 本质上异步的类似类型,因为在这些情况下,您可以从 UI 线程调用 Room 方法。

Room documentation了解更多信息。

关于android - 错误 : Cannot access database on the main thread since it may potentially lock the UI for a long period of time. - 使用 Kotlin 的 Android Room,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59607324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com