ios – rxSwift中的observable和subject之间有什么区别

前端之家收集整理的这篇文章主要介绍了ios – rxSwift中的observable和subject之间有什么区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
观察和主题之间有什么区别.
当我定义一个可观察的类型变量时.它可以发出onNext,onComplete,onDispose.但是主题也可以这样做.什么时候应该使用observable,在什么情况下我应该使用subject?

解决方法

为了理解它们之间的区别,我们应该提到 Observable是:

In ReactiveX an observer subscribes to an Observable. Then that
observer reacts to whatever item or sequence of items the Observable
emits. This pattern facilitates concurrent operations because it does
not need to block while waiting for the Observable to emit objects,
but instead it creates a sentry in the form of an observer that stands
ready to react appropriately at whatever future time the Observable
does so.

换句话说,可观察的是数据生产者(负责发布要观察的通知).

实际上,Subject是一种特殊类型的Observables(您仍然可以像任何其他可观察者一样订阅消息):

A Subject is a sort of bridge or proxy that is available in some
implementations of ReactiveX that acts both as an observer and as an
Observable. Because it is an observer,it can subscribe to one or more
Observables,and because it is an Observable,it can pass through the
items it observes by reemitting them,and it can also emit new items.

但是主题是一个表示 – 在可观察和观察者的文档中提到,这意味着主题可能是数据生成者(负责发布要观察的通知或数据使用者(负责接收通知)).

代码片段:我建议检查RxSwift For Dummies ? Part 3以查看可观察量和主题实用代码.

另外:要检查主题的类型,您可能需要检查:RxSwift Subject Types.

猜你在找的iOS相关文章