RxJava: “java.lang.IllegalStateException: Only one subscriber allowed!”

后端 未结 1 2012
轮回少年
轮回少年 2021-01-13 04:56

I\'m using RxJava to calculate the normalized auto correlation over some sensor data in Android. Strangely enough, my code throws an exception (\"java.lang.IllegalStateExcep

相关标签:
1条回答
  • 2021-01-13 05:42

    In RxJava, the operators groupBy and window return an observable which can be subscribed to only once and if subscribed, they replay their accumulated contents to the sole subscriber and switch to 'hot' mode.

    This was a tradeoff between returning a fully hot observable and risk missing values or return an unbounded replaying observable that allows any subscribers but retains the accumulated contents indefinitely.

    The middle ground, namely a single subscriber, cold-then-hot observable is thought to be the least surprising behavior and gives the developer the option to apply further operators and pick any point between the two extremes:

    source.window(1, TimeUnit.SECONDS)
        .map(w -> w.publish())
        .doOnNext(w -> w.connect())
        .subscribe(...)
    
    source.window(1, TimeUnit.SECONDS)
        .map(w -> w.cache())
        .subscribe(...)
    
    0 讨论(0)
提交回复
热议问题