web-dev-qa-db-ja.com

Observable.empty()によりJava.util.NoSuchElementExceptionが発生します:シーケンスに要素が含まれていません

RxJava1.0.14でRetrofit2.0.0-beta2を使用しています。 doFinallyでコードを実行する必要があるため、この方法でエラーを処理します。

.onErrorResumeNext(Observable.empty());

しかし、エラー(401など)でhttp応答を受け取ると、スタックトレースにクラスがない状態でアプリがクラッシュします。 Observable.neverを使用しても、問題は発生しません。完全なスタックトレースは次のとおりです。

Java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
at rx.internal.schedulers.ScheduledAction.run (ScheduledAction.Java:60)
at Android.os.Handler.handleCallback (Handler.Java:739)
at Android.os.Handler.dispatchMessage (Handler.Java:95)
at Android.os.Looper.loop (Looper.Java:135)
at Android.app.ActivityThread.main (ActivityThread.Java:5221)
at Java.lang.reflect.Method.invoke (Unknown source)
at Java.lang.reflect.Method.invoke (Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.Java:899)
at com.Android.internal.os.ZygoteInit.main (ZygoteInit.Java:694)

rx.exceptions.OnErrorNotImplementedException: Sequence contains no elements
at rx.Observable$27.onError (Observable.Java:7535)
at rx.observers.SafeSubscriber._onError (SafeSubscriber.Java:154)
at rx.observers.SafeSubscriber.onError (SafeSubscriber.Java:111)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.Java:70)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.Java:70)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.Java:70)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue (OperatorObserveOn.Java:197)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call (OperatorObserveOn.Java:170)
at rx.internal.schedulers.ScheduledAction.run (ScheduledAction.Java:55)
at Android.os.Handler.handleCallback (Handler.Java:739)
at Android.os.Handler.dispatchMessage (Handler.Java:95)
at Android.os.Looper.loop (Looper.Java:135)
at Android.app.ActivityThread.main (ActivityThread.Java:5221)
at Java.lang.reflect.Method.invoke (Unknown source)
at Java.lang.reflect.Method.invoke (Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.Java:899)
at com.Android.internal.os.ZygoteInit.main (ZygoteInit.Java:694)

Java.util.NoSuchElementException: Sequence contains no elements
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted (OperatorSingle.Java:131)
at rx.internal.operators.OperatorTake$1.onCompleted (OperatorTake.Java:53)
at rx.Observable$EmptyHolder$1.call (Observable.Java:1077)
at rx.Observable$EmptyHolder$1.call (Observable.Java:1074)
at rx.Observable.unsafeSubscribe (Observable.Java:7710)
at rx.internal.operators.OperatorOnErrorResumeNextViaObservable$1.onError (OperatorOnErrorResumeNextViaObservable.Java:76)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.Java:70)
at rx.internal.operators.OperatorSubscribeOn$1$1$1.onError (OperatorSubscribeOn.Java:71)
at rx.observers.SerializedObserver.onError (SerializedObserver.Java:159)
at rx.observers.SerializedSubscriber.onError (SerializedSubscriber.Java:79)
at rx.internal.operators.OperatorTakeUntil$1.onError (OperatorTakeUntil.Java:49)
at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError (OperatorMerge.Java:239)
at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate (OperatorMerge.Java:774)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop (OperatorMerge.Java:532)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emit (OperatorMerge.Java:521)
at rx.internal.operators.OperatorMerge$InnerSubscriber.onError (OperatorMerge.Java:808)
at rx.Observable$ThrowObservable$1.call (Observable.Java:9600)
at rx.Observable$ThrowObservable$1.call (Observable.Java:9590)
at rx.Observable.unsafeSubscribe (Observable.Java:7710)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext (OperatorMerge.Java:231)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext (OperatorMerge.Java:140)
at rx.internal.operators.OperatorMap$1.onNext (OperatorMap.Java:55)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call (RxJavaCallAdapterFactory.Java:113)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call (RxJavaCallAdapterFactory.Java:88)
at rx.Observable$2.call (Observable.Java:162)
at rx.Observable$2.call (Observable.Java:154)
at rx.Observable$2.call (Observable.Java:162)
at rx.Observable$2.call (Observable.Java:154)
at rx.Observable$2.call (Observable.Java:162)
at rx.Observable$2.call (Observable.Java:154)
at rx.Observable.unsafeSubscribe (Observable.Java:7710)
at rx.internal.operators.OperatorSubscribeOn$1$1.call (OperatorSubscribeOn.Java:62)
at rx.internal.schedulers.ScheduledAction.run (ScheduledAction.Java:55)
at Java.util.concurrent.Executors$RunnableAdapter.call (Executors.Java:422)
at Java.util.concurrent.FutureTask.run (FutureTask.Java:237)
at Java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201 (ScheduledThreadPoolExecutor.Java:152)
at Java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run (ScheduledThreadPoolExecutor.Java:265)
at Java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.Java:1112)
at Java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.Java:587)
at Java.lang.Thread.run (Thread.Java:818)

ありがとう、
アントン

14
Anton Shkurenko

スタックトレースから、

_at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted (OperatorSingle.Java:131)
at rx.internal.operators.OperatorTake$1.onCompleted (OperatorTake.Java:53)
_

これは、take(1).single()コンビニエンス演算子でもあるfirst()と同等です。後者を使用していると思われますが、どちらにも少なくとも1つの要素が必要です。

empty()を使用すると、ダウンストリーム演算子のコントラクトに違反する要素のないオブザーバブルを作成できます。 onErrorResumeNext observableでデフォルトのアイテムを発行するか、要素のないストリームをサポートするようにダウンストリームコードを変更します。

37
Jake Wharton