同一个Observer在几个Observable上调用.subscribe()
的行为是什么?
他们会被隐含地合并吗?或者是最后一次获胜?
例如:
event1.subscribe(myObserver);
event2.subscribe(myObserver);
答案 0 :(得分:1)
基于javadoc,只要您没有cancel每个Observable返回的Subscription
,观察者就会将订阅到所有可观察对象。
我希望如果您多次向同一个观察者订阅同一个观察者,您将会多次收到通知 - 否则之前订阅会“神奇地”死亡,这可能会导致麻烦。
// data members
Observable x, y;
void foo(Subscriber a) {
// is subscriber a already registered with x?
// Nevermind, I'm gonna make sure it is!
Subscription s=x.subscribe(a);
// provoke a change to be caught by 'a'
// do some work based on whatever Subscriber 'a' captured.
s.unsubscribe();
}
void bar() {
Subscriber m;
Subscription mx=x.subscribe(m);
Subscription my=y.subscribe(m);
// do something and then...
foo(mx); // one would expect the mx subscription is still valid here!!!
// do some more work
my.unsubscribe();
mx.unsubscribe();
}