为什么 Subject<T>.Dispose 不处理当前订阅? [英] Why does Subject<T>.Dispose does not dispose current subscriptions?

查看:27
本文介绍了为什么 Subject<T>.Dispose 不处理当前订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑 Subject 如果您手动调用它的 Dispose 方法,它会根据它处理所有订阅.但我最近发现它不能那样工作,它只是清除其内部观察者集合并将其替换为 DisposedObserver 助手类实例.

Hi I've been thinking for some time that Subject<T> disposes all the subscriptions based on it if you manually call its Dispose method. But I've recently found it doesn't work that way, it just clears its inner collection of observers and substitutes it with a DisposedObserver helper class instance.

我发现自己对这种行为有点困惑,只是假设正常"只会传播并处理所有订阅者.后来想弄清楚为什么会这样设计,我猜到了他们这样设计的几个原因.

I found myself a little confused about the behaviour, just assumed that the "normal" would be just propagate and dispose all the suscribers. Later, trying to figure out why is designed this way, I guessed a couple of reasons why they designed this way.

  • 订阅者可能是一个部分依赖于主题的组合,因此完全传播处置没有意义.IE.正如每个人所期望的那样,合并不会仅仅因为其中一个来源被处置而被处置.
  • Subject.Dispose 它在语义上等同于观察者一侧的 Observable.Never 延续.如果想在处理之前发出错误或完成信号,Subject.Dispose 调用方也可以调用 OnComplete 或 OnError(因为它们在同一范围内).
  • The suscriber may be a composition that depents partially on the subject , so full propagation of disposal doesn't make sense. ie. Merge is not disposed just because one of the sources was disposed, as everyone expects.
  • Subject.Dispose It is semantically equivalent to a continuation with Observable.Never from the side of the observer. The Subject.Dispose caller can also call OnComplete or OnError if wanted to signal error or completion before disposal (because they are on the same scope).

编辑注意:抱歉,问题不清楚.我已经了解如何使用它,这更像是一个设计问题.让我更清楚地说明一下.

Edit Note: Sorry for the unclear question. I already understand how to use it, this was more a design question. Let me state it more clearly.

你认为 Rx 的设计者为什么会这样处理 Dispose 行为?

Why do you think the designers of Rx made Dispose behaviour that way?

(以上两点是我的回答试炼)

(the two points above are my answer trial)

推荐答案

主题应该通过发送 OnComplete 或可能的 OnError 来表明它已完成.这是惯用和语法正确的 Rx.订阅者负责通过处置来结束他们的订阅.您应该编写 Observable,以便它们在完成"后清理资源,即使订阅者尚未取消订阅.

A subject should indicate it is done by sending OnComplete or possibly OnError. This is idiomatically and grammatically correct Rx. Subscribers are responsible for ending their subscriptions by disposing them. You should write Observables such that they clean up resources once they are "done" even if subscribers have not unsubscribed.

这篇关于为什么 Subject&lt;T&gt;.Dispose 不处理当前订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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