3

I have implemented an Observable class that stores a list all the observers. Should this list contain weak references to the observers for preventing memory leaks?

What is common practise?

4

2 回答 2

1

Not necessarily, it depends on the context. Some observers may just want to do simple things like logging and such, and would prefer to be kept alive by the Observable object. Best thing to do is just to clearly document your choice, so that observers know whether or not they need to explicitly deregister themselves.

于 2011-02-16T13:20:56.020 回答
0

题外话,如果您使用的是 Rx 而不仅仅是IObservable.NET 4 的接口,您应该将Subscribe调用映射到私有实例,Subject因为它将为您处理线程安全和调用顺序。然后,您可以调用On*主题实例。

就主题而言,订阅者通常管理自己的订阅。至少这意味着处理Subscribe返回值,但在 Rx 中,订阅通常会被另一个运算符(例如Takeor TakeUntil)终止。

于 2011-02-16T19:48:11.020 回答