重试获取数据:rx.exceptions.MissingBackpressureException

时间:2016-02-09 15:37:51

标签: android rx-java rx-android

我在生产版本(Market build)上遇到了这个崩溃。这么多用户受此次崩溃影响,但我无法重现它。

这是堆栈跟踪:

Fatal Exception: 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:145)
   at android.app.ActivityThread.main(ActivityThread.java:5832)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Caused by rx.exceptions.OnErrorNotImplementedException
   at rx.Observable$31.onError(Observable.java:7280)
   at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:154)
   at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
   at rx.observers.SerializedObserver.onError(SerializedObserver.java:122)
   at rx.observers.SerializedSubscriber.onError(SerializedSubscriber.java:79)
   at rx.android.app.OperatorConditionalBinding$1.onError(OperatorConditionalBinding.java:69)
   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:183)
   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:159)
   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:145)
   at android.app.ActivityThread.main(ActivityThread.java:5832)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

 Caused by rx.exceptions.MissingBackpressureException
   at rx.internal.util.RxRingBuffer.onNext(RxRingBuffer.java:349)
   at rx.internal.operators.OperatorZip$Zip$InnerSubscriber.onNext(OperatorZip.java:330)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.onNext(SubjectSubscriptionManager.java:224)
   at rx.subjects.PublishSubject.onNext(PublishSubject.java:114)
   at com.opensooq.OpenSooq.ui.BaseActivity$2.onClick(BaseActivity.java:562)
   at android.view.View.performClick(View.java:5156)
   at android.view.View$PerformClick.run(View.java:20755)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5832)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是我的代码:

public static final Func1 RETRY_CONDITION = new Func1<Observable<? extends Throwable>, Observable<?>>() {
    @Override
    public Observable<?> call(Observable<? extends Throwable> observable) {
        return observable.zipWith(NO_INTERENT_CLICKED_EVENT, new Func2<Throwable, Object, Object>() {
            @Override
            public Object call(Throwable throwable, Object o) {
                return throwable;
            }
        });
    }
};

public static final PublishSubject<Object> NO_INTERENT_CLICKED_EVENT = PublishSubject.create();

noInternetView.findViewById(R.id.bRetry).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noInternetView.findViewById(R.id.noInternetLoading).setVisibility(View.VISIBLE);
                    noInternetView.findViewById(R.id.llBody).setVisibility(View.GONE);
                    NO_INTERENT_CLICKED_EVENT.onNext(new Object());
                }
            });    

调用login observable:

  bindLifecycle(loginObservable
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    if (throwable instanceof RetrofitError) {
                        toggleNoInternetView(true);
                    }
                    hideLoader();
                }
            })
            .doOnNext(new Action1<LoginResult>() {
                @Override
                public void call(LoginResult loginResult) {
                 // Handle success
                }
            })
            .doOnCompleted(new Action0() {
                @Override
                public void call() {
                    hideLoader();
                    toggleNoInternetView(false);
                }
            })
            .retryWhen(BaseActivity.RETRY_CONDITION), LifecycleEvent.DESTROY)
            .subscribe();

我的代码在互联网连接丢失时重试获取数据。

请帮助解决此问题。

1 个答案:

答案 0 :(得分:2)

有两个失败需要考虑。第一个是OnErrorNotImplementedException,这意味着您使用的是subscribe(Action1)而不是subscribe(Action1, Action1),而且例外无法进入任何地方。

第二个失败是zip中的缓冲区溢出,可能是因为您没有在其输入上使用onBackpressureBufferonBackpressureDrop。由于您的示例中没有拉链,我无法说明为什么它不能使用点击事件。也许拉链的其他来源或拉链的下游更慢。也许你也把事件推到NO_INTERENT_CLICKED_EVENT其他地方。