RxJava2:第一个订阅和最后一个取消订阅的观察者的 PublishProcessor 回调
RxJava2: PublishProcessor callbacks for the first subscribed and the last unsubscribed observer
我使用 PublishProcessor<?>
为多个观察者提供服务。有没有办法知道第一个观察者何时被订阅以及最后一个观察者何时被处置?
不直接,需要抓取订阅者和取消者:
PublishProcessor<?> pp = ...
AtomicInteger counter = new AtomicInteger();
Action onFirst = ...
Action onLast = ...
Flowable<?> f = pp.doOnSubscribe(s -> {
if (counter.getAndIncrement() == 0) {
onFirst.run();
}
})
.doFinally(() -> {
if (counter.decrementAndGet() == 0) {
onLast.run();
}
})
// use f for subscribe() instead of pp
我使用 PublishProcessor<?>
为多个观察者提供服务。有没有办法知道第一个观察者何时被订阅以及最后一个观察者何时被处置?
不直接,需要抓取订阅者和取消者:
PublishProcessor<?> pp = ...
AtomicInteger counter = new AtomicInteger();
Action onFirst = ...
Action onLast = ...
Flowable<?> f = pp.doOnSubscribe(s -> {
if (counter.getAndIncrement() == 0) {
onFirst.run();
}
})
.doFinally(() -> {
if (counter.decrementAndGet() == 0) {
onLast.run();
}
})
// use f for subscribe() instead of pp