[英]RxJava: Dynamic set of observables
I have a central class, call it Central
. 我有一个中央班级,叫做Central
。 It can have 1-N observables added to it. 它可以添加1-N个可观察物。 These I need to add dynamically, and then know when the final onComplete() has executed. 这些我需要动态添加,然后知道何时执行最后的onComplete()。
How to do this? 这个怎么做?
Code sample: 代码示例:
public class Central {
public void addObservable(Observable o){
// Add the new observable to subscriptions
}
}
Update. 更新。
I've been going around on this one a while. 我已经花了一段时间了。 Using @DaveMoten's answer, I've gotten close. 使用@DaveMoten的答案,我已经接近了。
Here is my method to add a new observable at will, and be notified when all of them have completed (pseudo-code): 这是我的方法,可随意添加一个新的Observable,并在所有这些Observable完成时得到通知(伪代码):
class central {
PublishSubject<Observable<T>> subject = PublishSubject.create();
int commandCount = 0;
int concatCount = 0;
addObservable(Observable newObs){
commandCount++;
concatCount++;
subject.concatMap(o ->
o.doOnCompleted(
() -> {
concatCount--;
LOG.warn("inner completed, concatCount: " + concatCount);
})
).doOnNext(System.out::println)
.doOnCompleted(() -> {
System.out.println("OUTER completed");
} ) .subscribe();
onNext(newObs);
subject.onCompleted(); // If this is here, it ends prematurely (after first onComplete)
// if its not, OUTER onComplete never happens
newObs.subscribe((e) ->{},
(error) -> {},
() -> {
commandCount--;
LOG.warn("inner end: commandCount: " + commandCount);
});
}
}
// ... somewhere else in the app:
Observable ob1 = Observable.just(t1);
addObservable.addObservable(ob1);
// ... and possibly somewhere else:
Observable ob2 = Observable.just(t2, t3);
addObservable(ob2);
// Now, possibly somewhere else:
ob1.onNext(1);
// Another place:
ob2.onNext(2);
The logging looks like so: 日志记录如下所示:
19:51:16.248 [main] WARN execute: commandCount: 1
19:51:17.340 [main] WARN execute: commandCount: 2
19:51:23.290 [main] WARN execute: commandCount: 3
9:51:26.969 [main] WARN inner completed, concatCount: 2
19:51:27.004 [main] WARN inner end: commandCount: 2
19:51:27.008 [main] WARN inner completed, concatCount: 1
19:51:27.009 [main] WARN inner end: commandCount: 1
19:51:51.745 [ProcessIoCompletion0] WARN inner completed, concatCount: 0
19:51:51.750 [ProcessIoCompletion0] WARN inner completed, concatCount: -1
19:51:51.751 [ProcessIoCompletion0] WARN inner end: commandCount: 0
19:51:51.752 [ProcessIoCompletion0] WARN inner completed, concatCount: -2
19:51:51.753 [ProcessIoCompletion0] WARN inner completed, concatCount: -3
UPDATE: I added some counters, that demonstrate I don't understand what is happening with concatMap. 更新:我添加了一些计数器,这些计数器表明我不了解concatMap发生了什么。 You can see the lambda subscribe on the observers itself count down to 0 correctly, but the concatMap oncomplete goes down to -3! 您可以看到观察者本身的lambda订阅数正确地递减为0,但是concatMap oncomplete降至-3! And OUTER complete never happens. 而且OUTER完全不会发生。
Use a PublishSubject
: 使用PublishSubject
:
PublishSubject<Observable<T>> subject =
PublishSubject.create();
subject
// protect against concurrent calls to subject (optional)
.serialize()
.concatMap(o ->
o.doOnCompleted(() -> System.out.println("inner completed")))
.doOnNext(System.out::println)
.doOnCompleted(() -> System.out.println("completed"))
.subscribe(subscriber);
subject.onNext(Observable.just(t1));
subject.onNext(Observable.just(t2, t3));
subject.onCompleted();
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.