房间查询返回可能<list>在返回空结果

时间:2018-03-05 22:56:14

标签: android sql rx-java2 android-room maybe

我有房间查询:

@Query("SELECT * FROM classes WHERE _id IN(:values) ORDER BY date DESC")
fun getClassesByIds(values: List<Int>): Maybe<List<YClass>>
compositeDisposable.add(viewModel.getClassesById(classesTaken)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSuccess { list ->
                        list.forEach {

                            setupInfo(student)
                        }
                    }
                    .doOnError { it.printStackTrace() }
                    .subscribe())

当结果为空时,当我从活动导航回来时,应用程序崩溃。 实际上在执行另一个查询时发生崩溃。第二个查询是错误的,即使它没有任何问题。

  

引起:android.arch.persistence.room.EmptyResultSetException:   查询返回空结果集:SELECT * FROM classes WHERE _id IN()   ORDER BY date DESC

     

03-06 00:07:53.476 31132-31199 /? I / InputDispatcher:Window   &#39; Window {135734f u0   com.labfoodandfriends.nikitagudkovs.jlog / com.labfoodandfriends.nikitagudkovs.jlog.activity.student_and_teacher.StudentOverview}&#39;   花了4249.1ms处理最后一个输入事件:KeyEvent(deviceId = -1,   source = 0x00000101,action = 1,flags = 0x00000048,keyCode = 4,scanCode = 0,   metaState = 0x00000000,repeatCount = 0),policyFlags = 0x6b000002 03-06   00:07:53.612 13731-13736 / com.labfoodandfriends.nikitagudkovs.jlog   I / zygote64:编译器分配6MB来编译void   android.view.ViewRootImpl.performTraversals()03-06 00:07:53.661   13731-13736 / com.labfoodandfriends.nikitagudkovs.jlog I / zygote64:Do   完整代码缓存集合,代码= 502KB,数据= 339KB 03-06 00:07:53.662   13731-13736 / com.labfoodandfriends.nikitagudkovs.jlog I / zygote64:之后   代码缓存集合,代码= 501KB,数据= 295KB 03-06 00:07:55.873   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:   io.reactivex.exceptions.OnErrorNotImplementedException:返回查询   空结果集:SELECT * FROM类WHERE _id IN()ORDER BY日期   DESC 03-06 00:07:55.874   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.functions.Functions $ OnErrorMissingConsumer.accept(Functions.java:704)   03-06 00:07:55.875   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.functions.Functions $ OnErrorMissingConsumer.accept(Functions.java:701)   03-06 00:07:55.875   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:47)   03-06 00:07:55.876   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.operators.single.SingleDoOnError $ DoOnError.onError(SingleDoOnError.java:63)   03-06 00:07:55.876   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.operators.single.SingleDoOnSuccess $ DoOnSuccess.onError(SingleDoOnSuccess.java:64)   03-06 00:07:55.877   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.operators.single.SingleObserveOn $ ObserveOnSingleObserver.run(SingleObserveOn.java:79)   03-06 00:07:55.877   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.android.schedulers.HandlerScheduler $ ScheduledRunnable.run(HandlerScheduler.java:109)   03-06 00:07:55.878   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在android.os.Handler.handleCallback(Handler.java:790)03-06   00:07:55.878 13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog   W / System.err:at   android.os.Handler.dispatchMessage(Handler.java:99)03-06 00:07:55.878   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在android.os.Looper.loop(Looper.java:164)03-06 00:07:55.879   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在android.app.ActivityThread.main(ActivityThread.java:6494)03-06   00:07:55.879 13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog   W / System.err:at java.lang.reflect.Method.invoke(Native Method)   03-06 00:07:55.879   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)   03-06 00:07:55.880   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)03-06   00:07:55.881 13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog   W / System.err:引起:   android.arch.persistence.room.EmptyResultSetException:返回查询   空结果集:SELECT * FROM类WHERE _id IN()ORDER BY日期   DESC 03-06 00:07:55.881   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   com.labfoodandfriends.nikitagudkovs.jlog.database.dao.StudentDAO_Impl $ 6.call(StudentDAO_Impl.java:345)   03-06 00:07:55.882   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   com.labfoodandfriends.nikitagudkovs.jlog.database.dao.StudentDAO_Impl $ 6.call(StudentDAO_Impl.java:327)   03-06 00:07:55.882   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:44)   03-06 00:07:55.883   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在io.reactivex.Single.subscribe(Single.java:3096)03-06 00:07:55.883   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.operators.single.SingleSubscribeOn $ SubscribeOnObserver.run(SingleSubscribeOn.java:89)   03-06 00:07:55.884   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  at io.reactivex.Scheduler $ DisposeTask.run(Scheduler.java:463)03-06   00:07:55.884 13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog   W / System.err:at   io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)   03-06 00:07:55.885   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)   03-06 00:07:55.885   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)03-06   00:07:55.885 13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog   W / System.err:at   java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)   03-06 00:07:55.886   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)   03-06 00:07:55.886   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:636)   03-06 00:07:55.887   13731-13731 / com.labfoodandfriends.nikitagudkovs.jlog W / System.err:
  在java.lang.Thread.run(Thread.java:764)03-06 00:07:55.903   13731-13736 / com.labfoodandfriends.nikitagudkovs.jlog I / zygote64:Do   部分代码缓存集合,代码= 503KB,数据= 301KB 03-06   00:07:55.904 31132-15809 /? W / ActivityManager:强制完成   活动   com.labfoodandfriends.nikitagudkovs.jlog / .activity.students.StudentsActivity   03-06 00:07:55.909   13731-13736 / com.labfoodandfriends.nikitagudkovs.jlog I / zygote64:之后   代码缓存集合,代码= 503KB,数据= 301KB 03-06 00:07:55.909   13731-13736 / com.labfoodandfriends.nikitagudkovs.jlog I / zygote64:   将代码缓存容量增加到2MB 03-06 00:07:55.920 31132-31147 /?   I / ActivityManager:显示包的崩溃对话框   com.labfoodandfriends.nikitagudkovs.jlog u0 03-06 00:07:55.990   31132-1471 /? I / OpenGLRenderer:初始化的EGL,版本1.4 03-06   00:07:55.990 31132-1471 /? D / OpenGLRenderer:交换行为2 03-06   00:07:56.407 31132-31146 /? W / ActivityManager:活动暂停超时   对于ActivityRecord {395d736 u0   com.labfoodandfriends.nikitagudkovs.jlog / .activity.students.StudentsActivity   t7082 f}

2 个答案:

答案 0 :(得分:2)

对于可能对此问题感兴趣的人:

正如本文Room with Single explained

中所述

如果您使用MaybeSingle作为查询的返回类型,则当查询结果为空时,它将调用subscriber.onError(new EmptyResultSetException())。所以这完全按预期工作。

答案 1 :(得分:1)

如@insa_c所述,这是预期的行为。您可以做的是检查EmptyResultSetException并返回空容器。

    compositeDisposable.add(viewModel.getClassesById(classesTaken)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSuccess { list ->
                    list.forEach { 
                        setupInfo(student)
                    }
                }

           //You add this to return an empty set when an error occurs
            .onErrorResumeNext(error -> {
                if (error instanceof EmptyResultSetException) 
                     return Single.just(new ArrayList<>()); //<< an empty container is returned.
                else 
                     return Single.error(error);
            });                    

            //the rest of your stuffs.
                .doOnError { it.printStackTrace() } 
                .subscribe())