What are subjects
Subjects act as both an observable and an observer. You saw earlier how they can receive events and also be subscribed to. The subject received .next events, and each time it received an event, it turned around and emitted it to its subscriber.
受试者既可观察也可观察者。 你以前看到他们如何接收事件并且也被订阅。 该主题接收到下一个事件,并且每次收到一个事件时,它都会转向并将其发送到其用户。
There are four subject types in RxSwift:
RxSwift有四种主题类型:
- PublishSubject: Starts empty and only emits new elements to subscribers.
- PublishSubject:开始空,只向订阅者发出新的元素。
- BehaviorSubject: Starts with an initial value and replays it or the latest element to new subscribers.
- BehaviorSubject:从初始值开始,并将其重播或新订阅者的最新元素。
- ReplaySubject: Initialized with a buffer size and will maintain a buffer of elements up to that size and replay it to new subscribers.
- ReplaySubject:使用缓冲区大小初始化,并保持缓冲区大小的元素,并将其重新播放给新用户。
- Variable: Wraps a BehaviorSubject, preserves its current value as state, and replays only the latest/initial value to new subscribers.
- 变量:包裹一个BehaviorSubject,将其当前值保留为状态,并且只向最新/初始值重新发布新的订阅者。