BehaviourSubject的MissingBackPressure示例是什么?

时间:2017-07-27 09:45:52

标签: android rx-java rx-java2

在使用MissingBackPressure时,我有一个使用RxJava1.x的Android应用偶尔会抛出BehaviourSubject

它只发出一个列表,其中包含用于填充视图的对象。

    behaviourSubject.onNext(items);

但是,重现异常非常困难所以我正在尝试编写一个简单的测试,如下所示。

    BehaviorSubject<Object> p = BehaviorSubject.create((Object)1);
    p.test(0).assertFailure(MissingBackpressureException.class);

有人可以帮助这样的例子吗?感谢。

Caused by rx.exceptions.MissingBackpressureException
   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.onNext(SourceFile:160)
   at rx.internal.operators.NotificationLite.accept(SourceFile:135)
   at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(SourceFile:253)
   at rx.subjects.BehaviorSubject.onNext(SourceFile:160)
   ...
   at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(SourceFile:69)
   at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(SourceFile:77)
   at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(SourceFile:41)
   at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(SourceFile:38)
   at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(SourceFile:173)
   at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber$1.request(SourceFile:109)
   at rx.Subscriber.setProducer(SourceFile:211)
   at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.setProducer(SourceFile:105)
   at rx.Subscriber.setProducer(SourceFile:205)
   at rx.Subscriber.setProducer(SourceFile:205)
   at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(SourceFile:102)
   at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(SourceFile:102)
   at rx.Subscriber.setProducer(SourceFile:205)
   at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(SourceFile:152)
   at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(SourceFile:138)
   at rx.internal.operators.OnSubscribeLift.call(SourceFile:1048)
   at rx.Observable.unsafeSubscribe(SourceFile:10256)
   at rx.internal.operators.OnSubscribeMap.call(SourceFile:1048)
   at rx.Observable.unsafeSubscribe(SourceFile:10256)
   at rx.internal.operators.OnSubscribeMap.call(SourceFile:1048)
   at rx.internal.operators.OnSubscribeLift.call(SourceFile:1048)
   at rx.Observable.unsafeSubscribe(SourceFile:10256)
   at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(SourceFile:100)
   at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(SourceFile:230)
   at rx.internal.schedulers.ScheduledAction.run(SourceFile:55)

1 个答案:

答案 0 :(得分:1)

1.x BehaviorSubject忽略背压并在不考虑任何请求数量的情况下发出,因此它不会发出MissingBackpressureException信号。如果您在日志中看到此错误,则来自另一个运营商;这就是你应该总是发布故障堆栈跟踪的原因。

我不确定为什么你要为这个例外单独测试BehaviorSubject本身,但是最接近MissingBackpressureException的信号是PublishSubject

BehaviorSubject<Object> bs = BehaviorSubject.create((Object)1);

PublishSubject<Object> ps = PublishSubject.create();

AssertableSubscriber<Object> as = ps.test(0);

bs.subscribe(ps);

as.assertFailure(MissingBackpressureException.class);