做什么用的布置IObservers? [英] What to do with IObservers being disposed?

查看:178
本文介绍了做什么用的布置IObservers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是无扩展,便于事件处理在我的ViewModels(Silverlight和/或WP7应用程序)。为简单起见,假设我有一个这样的线在我的虚拟机的构造函数:

I'm using Reactive Extensions for easy event handling in my ViewModels (Silverlight and/or Wp7 apps). For sake of simplicity let's say I have a line like this in the ctor of my VM:

Observable.FromEvent<PropertyChangedEventArgs>(
                            h => MyObject.PropertyChanged += h, 
                            h => MyObject.PropertyChanged -= h)
    .Where(e=>e.PropertyName == "Title")
    .Throttle(TimeSpan.FromSeconds(0.5))
    .Subscribe(e=>{/*do something*/});

这会返回一个IDisposable的对象,这如果处置将取消。 (我是正确的这一假设?)
如果我认为没有提到它,迟早会被收集起来,而我的处理程序将被退订。

this returns an IDisposable object, which if disposed will unsubscribe. (Am I right in this assumption?)
If I hold no reference to it, sooner or later it will be collected, and my handler will be unsubscribed.

我通常有一个名单,其中,IDisposable的&GT; 在我的虚拟机,而我加入订阅它,但我觉得脏吧,好像我没有做的事情在一个正确的接收方式。

I usually have a List<IDisposable> in my VM, and I add subscriptions to it, yet I feel dirty about it, as if I'm not doing something in a correct Rx way.

什么是最好的做法,建议在情况格局这样吗?

推荐答案

您第一个假设是正确的,IDisposable的是退订。但是,你并不需要被收集持有引用了IDisposable,以prevent您的观察者。该的IObservable需要保持一个参考观察者能够调用它的方法,从而使观察者活着,只要观察到的是活的。

Your first assumption is correct, the IDisposable is for unsubscribing. However, you do not need to hold a reference to the IDisposable to prevent your observer from being collected. The IObservable will need to keep a reference to the observer to be able to call its methods, thus keeping the observer alive as long as the observable is alive.

细心的读者会发现,我有点回避问题,因为我认为可观测将不会被收集。为了解决这个问题,让我们这是怎么回事幕后一些合理的猜测。第一可观察被订阅的事件。这意味着,该事件的对象已经从我们订阅我们取消了时间的时候提到我们的观察者。由于接收运营商必须在某个时候订阅他们的资金来源,可以假设事件观察有参考凡观察者,其中有一个参考油门观察者,这当然指的是我们最终的观察者。

The astute reader will realize that I am somewhat begging the question, because I have assumed that the observable will not be collected. To deal with that issue, let's make some reasonable guesses about what's going on behind the scenes. The first observable is subscribing to an event. This means that the object with the event has a reference to our observer from the time we subscribe to the time we unsubscribe. Since Rx operators must at some point subscribe to their sources, one can assume that the event observer has a reference to the Where observer, which has a reference to the Throttle observer, which of course is referring to our final observer.

由于我们一直没有办法取消,这关系观察者的生活与活动对象的生活。如果没有充分了解你的程序,也就是所有的更远环比上涨,我可以去,但我觉得应该是够你确定观察到的寿命。

Since we kept no way to unsubscribe, this ties the life of the observer to the life of the object with the event. Without full knowledge of your program, that is all the farther up the chain I can go, but I think it should be sufficient for you to determine the lifetime of the observable.

这实际上指出了一个潜在的内存泄漏,你可以有标准.NET事件,对象保持其所有活动的用户还活着,这会导致你的第二个问题。如果你不保持一个引用了IDisposable,你将永远无法从事件退订和你的对象将继续得到通知,关闭它关系到视图后还是一样。如果事件源不超过视图长寿,这可能不是一个问题,但我建议使用一次性

This actually points out a potential "memory leak" that you can have with standard .NET events, objects keeping all their event subscribers alive, which leads to your second question. If you do not keep a reference to the IDisposable, you will never be able to unsubscribe from the event and your object will continue to receive notices, even after you close the view it is related to. If the event source does not live longer than the view, this may not be a problem, but I recommend using the disposable.

没有什么未接收关于这一点,和Rx甚至还包括一个不错的类来替代,如果你想使用的目录, System.Reactive.Disposables.CompositeDisposable

There is nothing "un-Rx" about this, and Rx even includes a nice class to use as an alternative to the List if you want, System.Reactive.Disposables.CompositeDisposable.

这篇关于做什么用的布置IObservers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆