WorkManager:无法访问主线程上的数据库,因为它可能会长时间锁定 UI
WorkManager : Cannot access database on the main thread since it may potentially lock the UI for a long period of time
我在使用 WorkManager 时出错。
Cannot access database on the main thread since it may potentially
lock the UI for a long period of time.
我的代码
val x = WorkManager.getInstance().getStatusesByTag(vm.workTag)
x.observeForever { }
如果我不使用方法观察一切正常
我的日志
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:223)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:269)
at androidx.work.impl.model.WorkSpecDao_Impl.compute(WorkSpecDao_Impl.java:928)
at androidx.work.impl.model.WorkSpecDao_Impl.compute(WorkSpecDao_Impl.java:914)
at androidx.lifecycle.ComputableLiveData.run(ComputableLiveData.java:101)
at androidx.arch.core.executor.testing.InstantTaskExecutorRule.executeOnDiskIO(InstantTaskExecutorRule.java:38)
at androidx.arch.core.executor.ArchTaskExecutor.executeOnDiskIO(ArchTaskExecutor.java:96)
at androidx.arch.core.executor.ArchTaskExecutor.execute(ArchTaskExecutor.java:53)
at androidx.lifecycle.ComputableLiveData.onActive(ComputableLiveData.java:70)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:418)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:214)
at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
at androidx.lifecycle.MediatorLiveData.onActive(MediatorLiveData.java:118)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:418)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:214)
at org.kadance.ftvideo.view.VideoPlayerActivity.onCreate(VideoPlayerActivity.kt:827)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:667)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
我运行Activity抛出测试class什么有InstantTaskExecutorRule.
androidTestImplementation 'androidx.arch.core:core-testing:2.0.0-beta01' 案例到这个错误。
我从我的测试中删除了 InstantTaskExecutorRule class,现在可以使用了
我在使用 WorkManager 时出错。
Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
我的代码
val x = WorkManager.getInstance().getStatusesByTag(vm.workTag)
x.observeForever { }
如果我不使用方法观察一切正常
我的日志
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:223)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:269)
at androidx.work.impl.model.WorkSpecDao_Impl.compute(WorkSpecDao_Impl.java:928)
at androidx.work.impl.model.WorkSpecDao_Impl.compute(WorkSpecDao_Impl.java:914)
at androidx.lifecycle.ComputableLiveData.run(ComputableLiveData.java:101)
at androidx.arch.core.executor.testing.InstantTaskExecutorRule.executeOnDiskIO(InstantTaskExecutorRule.java:38)
at androidx.arch.core.executor.ArchTaskExecutor.executeOnDiskIO(ArchTaskExecutor.java:96)
at androidx.arch.core.executor.ArchTaskExecutor.execute(ArchTaskExecutor.java:53)
at androidx.lifecycle.ComputableLiveData.onActive(ComputableLiveData.java:70)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:418)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:214)
at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
at androidx.lifecycle.MediatorLiveData.onActive(MediatorLiveData.java:118)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:418)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:214)
at org.kadance.ftvideo.view.VideoPlayerActivity.onCreate(VideoPlayerActivity.kt:827)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:667)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
我运行Activity抛出测试class什么有InstantTaskExecutorRule.
androidTestImplementation 'androidx.arch.core:core-testing:2.0.0-beta01' 案例到这个错误。
我从我的测试中删除了 InstantTaskExecutorRule class,现在可以使用了