RX android Single<Boolean> 在进行 ROOM 查询时不订阅 Schedulers.io() 线程而是订阅主线程
RX android Single<Boolean> not subscribing to Schedulers.io() thread but to main thread instead when doing ROOM query
我在使用android RX 方面不是很有经验。我正在使用此 query
检查实体是否退出 ROOM
这是我的 DAO
@Query("SELECT EXISTS(SELECT * FROM contacts WHERE number = :phoneNumber)")
fun exists(phoneNumber: Long) : Boolean
这是在我的 RepoImpl 中
override fun checkContactExists(number: Long): Single<Boolean> {
val exists = database.ContactsDao().exists(number)
return Single.fromCallable{
exists
}
}
我称这是这样的主持人:
repo.checkContactExists(number)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : DisposableSingleObserver<Boolean>(){
override fun onSuccess(exists: Boolean) {
Log.d(TAG, "contacts exists $exists")
if (exists) {
....
} else {
....
}
}
override fun onError(e: Throwable) {
Log.e(TAG, "failed to check if number exists ${e.message}")
}
})
当我调用它时,我得到了这个异常
2021-03-09 10:22:04.052 13966-13966/com.umony.radar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.umony.radar, PID: 13966
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:261)
at androidx.room.RoomDatabase.query(RoomDatabase.java:303)
at androidx.room.util.DBUtil.query(DBUtil.java:54)
任何人都可以帮助我理解为什么当我有 subscribeOn(Schedulers.io())
时在主线程上执行查询
更正您的方法 checkContactExists
,如下所示。它会起作用。
override fun checkContactExists(number: Long): Single<Boolean> {
return Single.fromCallable{
database.ContactsDao().exists(number)
}
}
我在使用android RX 方面不是很有经验。我正在使用此 query
这是我的 DAO
@Query("SELECT EXISTS(SELECT * FROM contacts WHERE number = :phoneNumber)")
fun exists(phoneNumber: Long) : Boolean
这是在我的 RepoImpl 中
override fun checkContactExists(number: Long): Single<Boolean> {
val exists = database.ContactsDao().exists(number)
return Single.fromCallable{
exists
}
}
我称这是这样的主持人:
repo.checkContactExists(number)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : DisposableSingleObserver<Boolean>(){
override fun onSuccess(exists: Boolean) {
Log.d(TAG, "contacts exists $exists")
if (exists) {
....
} else {
....
}
}
override fun onError(e: Throwable) {
Log.e(TAG, "failed to check if number exists ${e.message}")
}
})
当我调用它时,我得到了这个异常
2021-03-09 10:22:04.052 13966-13966/com.umony.radar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.umony.radar, PID: 13966
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:261)
at androidx.room.RoomDatabase.query(RoomDatabase.java:303)
at androidx.room.util.DBUtil.query(DBUtil.java:54)
任何人都可以帮助我理解为什么当我有 subscribeOn(Schedulers.io())
更正您的方法 checkContactExists
,如下所示。它会起作用。
override fun checkContactExists(number: Long): Single<Boolean> {
return Single.fromCallable{
database.ContactsDao().exists(number)
}
}