Room query returning Maybe<List> crashes app when returns empty result
Asked Answered
A

2

6

I have a room query:

@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())

When the result is empty app crashes when I navigate back from the activity. Actually crash happens when another query is being executed. THat second query is erroring even though there's nothing wrong with it.

Caused by: android.arch.persistence.room.EmptyResultSetException: Query returned empty result set: SELECT * FROM classes WHERE _id IN() ORDER BY date DESC

03-06 00:07:53.476 31132-31199/? I/InputDispatcher: Window 'Window{135734f u0 com.labfoodandfriends.nikitagudkovs.jlog/com.labfoodandfriends.nikitagudkovs.jlog.activity.student_and_teacher.StudentOverview}' spent 4249.1ms processing the last input event: 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: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals() 03-06 00:07:53.661 13731-13736/com.labfoodandfriends.nikitagudkovs.jlog I/zygote64: Do full code cache collection, code=502KB, data=339KB 03-06 00:07:53.662 13731-13736/com.labfoodandfriends.nikitagudkovs.jlog I/zygote64: After code cache collection, code=501KB, data=295KB 03-06 00:07:55.873 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err: io.reactivex.exceptions.OnErrorNotImplementedException: Query returned empty result set: SELECT * FROM classes WHERE _id IN() ORDER BY date DESC 03-06 00:07:55.874 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at 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:
at 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:
at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:47) 03-06 00:07:55.876 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at 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:
at 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:
at 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:
at 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:
at 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:
at android.os.Looper.loop(Looper.java:164) 03-06 00:07:55.879 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at 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:
at 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:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 03-06 00:07:55.881 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err: Caused by: android.arch.persistence.room.EmptyResultSetException: Query returned empty result set: SELECT * FROM classes WHERE _id IN() ORDER BY date DESC 03-06 00:07:55.881 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at 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:
at 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:
at 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:
at io.reactivex.Single.subscribe(Single.java:3096) 03-06 00:07:55.883 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at 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:
at 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:
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 03-06 00:07:55.886 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 03-06 00:07:55.887 13731-13731/com.labfoodandfriends.nikitagudkovs.jlog W/System.err:
at java.lang.Thread.run(Thread.java:764) 03-06 00:07:55.903 13731-13736/com.labfoodandfriends.nikitagudkovs.jlog I/zygote64: Do partial code cache collection, code=503KB, data=301KB 03-06 00:07:55.904 31132-15809/? W/ActivityManager: Force finishing activity com.labfoodandfriends.nikitagudkovs.jlog/.activity.students.StudentsActivity 03-06 00:07:55.909 13731-13736/com.labfoodandfriends.nikitagudkovs.jlog I/zygote64: After code cache collection, code=503KB, data=301KB 03-06 00:07:55.909 13731-13736/com.labfoodandfriends.nikitagudkovs.jlog I/zygote64: Increasing code cache capacity to 2MB 03-06 00:07:55.920 31132-31147/? I/ActivityManager: Showing crash dialog for package com.labfoodandfriends.nikitagudkovs.jlog u0 03-06 00:07:55.990 31132-1471/? I/OpenGLRenderer: Initialized EGL, version 1.4 03-06 00:07:55.990 31132-1471/? D/OpenGLRenderer: Swap behavior 2 03-06 00:07:56.407 31132-31146/? W/ActivityManager: Activity pause timeout for ActivityRecord{395d736 u0 com.labfoodandfriends.nikitagudkovs.jlog/.activity.students.StudentsActivity t7082 f}

Antonetta answered 5/3, 2018 at 22:56 Comment(4)
You might want to post the entire Java stack trace.Teressaterete
@Teressaterete updatedAntonetta
Did you try changing the return type from Maybe<List<YClass>> to Single<List<YClass>>? Given that you are expecting a List, you don't really need to make it Maybe, as it should return the expected empty list with a Single. (if that works, we can figure out why Maybe doesn't work)Sileas
@Sileas Single and Maybe work the same way here. I changed it to Flowable, which fixed my issue but is not an optimal choice as I only make this request once.Antonetta
M
8

as @insa_c mentioned, this is the expected behavior. what you can do is to check for the EmptyResultSetException and return the empty container instead.

    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())
Monarchy answered 4/5, 2019 at 11:26 Comment(0)
D
6

For people who might be interested in this issue:

As explained in this post Room with Single explained

if you use a Maybe or a Single as return type from an query, it'll call subscriber.onError(new EmptyResultSetException()) when the result from the query is empty. So that's completely working as expected.

Dendrite answered 12/6, 2018 at 18:34 Comment(1)
Maybe never call EmptyResultSetException(), just returns no rows and complateEdiva

© 2022 - 2024 — McMap. All rights reserved.