Noob Noob - 1 month ago 22
C# Question

Reactive Observable Subscription Disposal

If I have access to an IObservable that I know is only ever going to return one item, will this work and is it the best usage pattern?

IDisposable disposable = null;
disposable = myObservable.Subscribe(x =>
{
DoThingWithItem(x);
if (disposable != null)
{
disposable.Dispose();
}
});

Answer

Disclaimer: I'm also still learning Rx. So I'm not really an expert but I believe the disposable returned by Subscribe will only unsubscribe the subscription. Also if the source completes, like in your case, the unsubscription is done automatically. So I think the Dispose there is redundant and can be safely removed.

See the answer to this question for more info.