gpt4 book ai didi

java - 卡住了设置 Room 以与 RxJava 一起使用 : access to database may potentially lock the UI

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:15 26 4
gpt4 key购买 nike

所以我试图了解如何 react 以及如何使用 Room 库,但我遇到了一些异常,我不知道如何解决:

08-11 20:35:44.349 8797-8797/ru.android_school.h_h.eightapp W/System.err: io.reactivex.exceptions.OnErrorNotImplementedException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
at io.reactivex.internal.subscribers.LambdaSubscriber.onError(LambdaSubscriber.java:79)
at io.reactivex.internal.subscribers.LambdaSubscriber.onNext(LambdaSubscriber.java:69)
08-11 20:35:44.350 8797-8797/ru.android_school.h_h.eightapp W/System.err: at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.onNext(FlowableSubscribeOn.java:97)
at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnSubscriber.runAsync(FlowableObserveOn.java:400)
at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
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.
08-11 20:35:44.351 8797-8797/ru.android_school.h_h.eightapp W/System.err: at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:204)
at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:232)
at ru.android_school.h_h.eightapp.note_trio.NoteDao_Impl.getBySearch(NoteDao_Impl.java:230)
at ru.android_school.h_h.eightapp.note_list.ListActivity$1.onMenuItemActionCollapse(ListActivity.java:59)
at android.support.v7.view.menu.MenuItemImpl.collapseActionView(MenuItemImpl.java:841)
at ru.android_school.h_h.eightapp.note_list.ListActivity$4.accept(ListActivity.java:111)
at ru.android_school.h_h.eightapp.note_list.ListActivity$4.accept(ListActivity.java:107)
at io.reactivex.internal.subscribers.LambdaSubscriber.onNext(LambdaSubscriber.java:65)
... 11 more

在MainActivity的onCreate()中引起的:

db.noteDao()
.getAllLive()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<List<Note>>() {
@Override
public void accept(List<Note> notes) throws Exception {
progressDialog.dismiss();
searchMenuItem.collapseActionView();
listOfNotes.clear();
listOfNotes.addAll(notes);
listAdapter.notifyDataSetChanged();
if (listOfNotes.size() != 0) {
findViewById(R.id.emptyListReplacer).setVisibility(View.INVISIBLE);
} else {
findViewById(R.id.emptyListReplacer).setVisibility(View.VISIBLE);
}
}
});

其中 db 显然是一个数据库,而 noteDao 实现了 Dao 接口(interface)。我是响应式(Reactive)编程和多线程的新手,我不知道如何解决它。请帮忙?

UPD:在databaseBuilder中使用.allowMainThreadQueries()可以解决问题,但是,据我了解,这是解决问题的错误方法。

最佳答案

正确的代码是:

db.noteDao()
.getAllLive()
.subscribeOn(Schedulers.io()) // <-- subscribe to upstream task on io
.observeOn(AndroidSchedulers.mainThread()) // <-- observe downstream result back on UI
.subscribe(new Consumer<List<Note>>() {...});

如果您当前像在帖子中那样在更多地方访问数据库,那么如果您想在 io 上执行数据库操作并且订阅中的所有使用者都在 UI 线程上,请使用此线程方法。

关于java - 卡住了设置 Room 以与 RxJava 一起使用 : access to database may potentially lock the UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801505/

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