db.activitiesDao().insertStep(step);
这将返回臭名昭著的错误:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
我是 RxJava 的新手,不想使用 AsyncTask。
最佳答案
尝试这样的事情。
Observable.fromCallable(() -> db.activitiesDao().insertStep(step))
.subscribeOn(Schedulers.io())
.subscribe(...);
或者如果有 void return 你可以这样做:
Completable.fromRunnable(new Runnable(){
db.activitiesDao().insertStep(step)
})
.subscribeOn(Schedulers.io())
.subscribe(...);
关于android - 如何使用 Room 和 RxJava 插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47652371/