各种 ISubject 实现有什么作用以及何时使用它们? [英] What do the various ISubject implementations do and when would they be used?

查看:42
本文介绍了各种 ISubject 实现有什么作用以及何时使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Subject 类的作用以及何时使用它有一个很好的了解,但我刚刚浏览了 msdn 上的语言参考,看到还有其他各种 ISubject 实现,例如:

I have a fairly good idea of what the Subject class does and when to use it, but I've just been looking through the language reference on msdn and see there are various other ISubject implementations such as:

  • 异步主题
  • 行为主题
  • 重播主题

由于文档非常薄弱,每种类型的意义是什么,您会在什么情况下使用它们?

As the documentation is pretty thin on the ground, whats the point of each of these types and under what situations would you use them?

推荐答案

这些主题都有一个共同的属性——他们获取一些(或全部)通过 OnNext 发布给他们的内容并记录下来并播放给你——即他们采用 Hot Observable 并将其设为 Cold.这意味着,如果您多次订阅其中任何一个(即订阅 => 取消订阅 => 再次订阅),您将再次看到至少一个相同的值.

These subjects all share a common property - they take some (or all) of what gets posted to them via OnNext and record it and play it back to you - i.e. they take a Hot Observable and make it Cold. This means, that if you Subscribe to any of these more than once (i.e. Subscribe => Unsubscribe => Subscribe again), you'll see at least one of the same value again.

ReplaySubject:每次您订阅主题时,您都会尽快(或一个子集,如上一个n 项)

ReplaySubject: Every time you subscribe to the Subject, you get the entire history of what has been posted replayed back to you, as fast as possible (or a subset, like the last n items)

AsyncSubject:始终播放最后发布并完成的项目,但仅在源完成后播放.这个主题对于异步函数来说非常棒,因为你可以编写它们而不必担心竞争条件:即使有人在异步方法完成后订阅,他们也会得到结果.

AsyncSubject: Always plays back the last item posted and completes, but only after the source has completed. This Subject is awesome for async functions, since you can write them without worrying about race conditions: even if someone Subscribes after the async method completes, they get the result.

BehaviorSubject:有点像 ReplaySubject,但缓冲区为 1,所以你总是得到最后发布的内容.您还可以提供初始值.始终在订阅时立即提供一项.

BehaviorSubject: Kind of like ReplaySubject but with a buffer of one, so you always get the last thing that was posted. You also can provide an initial value. Always provides one item instantly on Subscribe.

这篇关于各种 ISubject 实现有什么作用以及何时使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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