同一个观察者在几个可观察对象上调用.subscribe()
的行为是什么?
它们会隐式合并吗?还是最后一个人赢了?
例如:event1.subscribe(myObserver);
event2.subscribe(myObserver);
根据javadoc,只要不取消每个observable返回的Subscription
s,该观察者将被订阅给所有的observable 。
我希望如果你多次订阅同一个观察者到一个可观察对象,你会得到多次通知——否则之前的订阅会"神奇地"死亡,这可能会引起麻烦。
// 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();
}