Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Correct use of Single with control flow in RxJava2

Tags: flow correctly

vkislicins:

I have the following Rx chain in my code:


public Single> dispatchStuff(){
return mRepository.getList()
.filter(list -> list.size() != 0)
.flatMapSingle(mRepository::operateOnList())
.doOnSuccess(mRepository::deleteList())
.subscribeOn(mSchedulerProvider.io())
.observeOn(mSchedulerProvider.ui());
}

However, I'm not sure I'm using the filtering Correctly here. The getList() method will always return a value or error, therefore I think using Single is acceptable. However, then I want to perform other operations only if there are elements in the list returned. If I filter Single and my list size is 0, it will through an exception:


java.util.NoSuchElementException
at io.reactivex.internal.operators.maybe.MaybeFlatMapSingle$FlatMapMaybeObserver.onComplete(MaybeFlatMapSingle.java:106)
at io.reactivex.internal.operators.maybe.MaybeFilterSingle$FilterMaybeObserver.onSuccess(MaybeFilterSingle.java:92)
at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:37)
at io.reactivex.Single.subscribe(Single.java:2692)
at io.reactivex.internal.operators.maybe.MaybeFilterSingle.subscribeActual(MaybeFilterSingle.java:40)
at io.reactivex.Maybe.subscribe(Maybe.java:3707)
at io.reactivex.internal.operators.maybe.MaybeFlatMapSingle.subscribeActual(MaybeFlatMapSingle.java:47)
at io.reactivex.Single.subscribe(Single.java:2692)
at io.reactivex.internal.operators.single.SingleDoOnSuccess.subscribeActual(SingleDoOnSuccess.java:35)
at io.reactivex.Single.subscribe(Single.java:2692)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
at io.reactivex.Scheduler$1.run(Scheduler.java:138)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

How should I correctly implement this control Flow when I'm working with Single?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Correct use of Single with control flow in RxJava2

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×