所以我试图理解如何反应以及如何使用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显然是实现Dao接口的数据库和noteDao。我刚接触反应式编程和多线程,但我不知道如何解决它。请帮帮忙?
更新:在databaseBuilder中使用.allowMainThreadQueries()解决了这个问题,但据我所知,这是解决问题的错误方法。
发布于 2018-08-12 00:57:36
正确的代码应该是:
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线程上,那么可以使用这种线程方法。
发布于 2018-08-12 17:32:35
异常不是由这段代码引起的。问题出在方法insert()和getBySearch()中,我在问题中没有指定它们。
发布于 2019-07-18 18:55:47
在我的例子中,假设的函数.getAllLive()是最简单的,只访问数据库,线程检查在主线程中执行(就像在主线程中构造管道的所有情况一样),在我的房间自动生成的代码中是:
@Override
public Completable markAsOutOfRoom(final int idRoom) {
final SupportSQLiteStatement _stmt = __preparedStmtOfMarkAsOutOfRoom.acquire();
int _argIndex = 1;
_stmt.bindLong(_argIndex, idRoom);
return Completable.fromCallable(new Callable<Void>() { ... } );
}但是,线程检查是在第一行__preparedStmtOfMarkAsOutOfRoom.acquire();上执行的,房间会抛出该异常。嗯,在几乎所有的情况下这都不是问题,这个获取不会抛出异常:java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.,但它可能碰巧抛出这个异常,可能是你的代码中有一个错误,但在我的例子中,代码是最简单的,不会有错误(至少在我的代码中)。可能是android内部的原因,不管怎样,如果你有这个问题,我提出一个临时的解决方案:
getCompositeDisposable().add(
Single.fromCallable(() ->
db.noteDao()
.getAllLive()
)
.subscribeOn(Schedulers.io())
.subscribe(s ->
getCompositeDisposable.add(
s.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(allLive -> { /* do anything with the result */ } )
)
)
);它创建一个可完成的,执行所需执行的流水线的惰性创建。
https://stackoverflow.com/questions/51801505
复制相似问题