Up until now I have zealously kept every reference to the IDisposable
returned from any .Subscribe(...)
, .Connect(...)
, etc, method within Rx. I've done this because of my fear that a garbage collections will dispose the disposable if I don't keep the reference.
However, I did a test in LINQPad where I did some calls GC.Collect()
on a .Subscribe(...)
where I didn't keep the reference and guess what? The world didn't end and the subscription ran to completion.
In further testing I found that my subscription was disposed of immediately after .OnComplete()
without my intervention.
This leads me to understand that, at least for .Subscribe(...)
, that the only reason to keep a reference to the subscription is to force the subscription to end before its normal completion. It's more like a cancellation token.
So are all Rx disposables used for cancelling rather than keeping alive?
What, then, are the rules for hanging on to an IDisposable
?