如何取消订阅PublishSubject和BehaviorSubject? [英] How can PublishSubject and BehaviorSubject be unsubscribed from?
问题描述
在 subject
包下,你有类似 PublishSubject
和 BehaviorSubject
我认为可以将其描述为一些可用的样本 Observables
。
Under the subjects
package you have classes like PublishSubject
and BehaviorSubject
which I suppose can be described as some usable sample Observables
.
如何取消订阅这些主题?没有取消订阅
方法并且调用 onCompleted
完全结束了Observable吗?
How can these subjects be unsubscribed from? There is no unsubscribe
method and calling onCompleted
ends the Observable altogether right?
推荐答案
主题是 Observable
和 Observer
at at同时,它可以像普通的可观察者一样取消订阅。使主题变得特别的是它在可观察者和观察者之间的桥梁。它可以通过重新发送它们来传递它所观察到的物品,它也可以发出新物品。由于承诺是期货,所以主题是可观察的。
A Subject is an Observable
and an Observer
at the same time, it can be unsubscribed from just like normal observables. what makes subject special is that it is sort of bridge between observables and observers. it can pass through the items it observes by reemitting them, and it can also emit new items. subjects are to observables as promises are to futures.
以下是科目家庭的简要说明:
here is a brief description of the subjects family:
AsyncSubject :仅限发出源的最后一个值Observable
AsyncSubject: only emits the last value of the source Observable
BehaviorSubject :当观察者发出最近发出的项目和源Observable的所有后续项目订阅
BehaviorSubject: emits the most recently emitted item and all the subsequent items of the source Observable when a observer subscribe to it
PublishSubject :在订阅时发出源Observable的所有后续项目
PublishSubject: emits all the subsequent items of the source Observable at the time of the subscription
ReplaySubject :无论订阅者何时订阅,都会发出源Observable的所有项目。
ReplaySubject: emits all the items of the source Observable, regardless of when the subscriber subscribes.
官方文档附带一些漂亮的大理石图表,使其更容易理解
the official doc comes with some nice marble diagrams which makes it more easier to understand
这篇关于如何取消订阅PublishSubject和BehaviorSubject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!