无功观测认购处置 [英] Reactive Observable Subscription Disposal

查看:163
本文介绍了无功观测认购处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有机会获得的的IObservable,我知道只有以往任何时候都返回一个项目,将这项工作,它是最好的使用模式?

  IDisposable的一次性= NULL;
一次性= myObservable.Subscribe(X =>
  {
     DoThingWithItem(X);
     如果(弃!= NULL)
     {
       disposable.Dispose();
     }
  });
 

解决方案

免责声明:我也还在学习接收。所以我不是一个真正的专家,但我相信,通过一次性返回订阅将只取消订阅。此外,如果源完成,像你的情况,将取消订阅是自动完成的。所以我觉得处置有多余的,可以安全地删除。

一看便知这个问题了解更多信息。

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();
     }
  });

解决方案

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.

这篇关于无功观测认购处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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