问题描述
我正在使用Reactive Extensions(Rx)主题直接替代C#事件,例如:
I am using the Reactive Extensions (Rx) Subject as a direct replacement for C# events like so:
public class MyClass
{
private Subject<string> subject;
public IObservable<string> WhenSomethingHappened
{
get { return this.subject.AsObservable(); }
}
private void OnSomethingHappened(string something)
{
this.subject.OnNext(something);
}
}
请注意,我从不会在主题上调用OnCompleted。 MyClass是否应该实现IDisposable并调用this.subject.Dispose?这意味着任何使用Subject的实现都应实现IDisposable。
Note that I never call OnCompleted on my subject. Should MyClass be implementing IDisposable and calling this.subject.Dispose? This would mean that any implementation using Subject should implement IDisposable.
我问的原因是IDisposable模式有点像疾病,如果一件事实现了它,那么使用它的所有事物也必须实现它。
The reason I ask is that the IDisposable pattern is a bit like a disease, if one thing implements it, everything that uses it has to implement it too.
推荐答案
不是,您实际上不需要这样做。在担心内存使用和生存期时,请考虑处理 Subscriptions ,而不是主题。
Nope, you don't really need to do this. When worrying about memory usage and lifetimes, think about disposing Subscriptions, not Subjects.
这篇关于我是否应该打电话给Disactive on Reactive Extensions(Rx)Subject< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!