1. Data sequence
Observable represents all Changes to your data as an event sequence that go through time.
You don't need to care about the changes, The data tell you when it changes and you just need to react to these changes.
we can subscribe to observe this kind of data and build your logic around that.
2. Observables and Observers
- here is an example for UISwitch
When we toggle this Switch, it emits the bool value of (True or False)
3. Operators
You can use different methods on these Observable class
. similar with the methods that you have on any sequence.
- example
filter()
I want to react only to the true state. So you can filter out all of the events that are true.
map()
If you want change the Bool states of the UISwitch to another type.
1. Demo1
//
1. Variable
Variable can hold only one value, it can be a String or an Array.
2. Event
1. Composing Sequence
RxRelay
RxRelay 既是 可監(jiān)聽序列 也是 觀察者录豺。
他和 Subjects 相似鹅颊,唯一的區(qū)別是不會接受 onError 或 onCompleted 這樣的終止事件弯淘。
在將非 Rx 樣式的 API 轉(zhuǎn)化為 Rx 樣式時,Subjects 是非常好用的神得。不過一旦 Subjects 接收到了終止事件 onError 或 onCompleted。他就無法繼續(xù)工作了,也不會轉(zhuǎn)發(fā)后續(xù)任何事件纵寝。有些時候這是合理的攒岛,但在多數(shù)場景中這并不符合我們的預(yù)期赖临。
在這些場景中一個更嚴(yán)謹(jǐn)?shù)淖龇ň褪牵瑒?chuàng)造一種特殊的 Subjects灾锯,這種 Subjects 不會接受終止事件兢榨。有了他,我們將 API 轉(zhuǎn)化為 Rx 樣式時顺饮,就不必?fù)?dān)心一個意外的終止事件吵聪,導(dǎo)致后續(xù)事件轉(zhuǎn)發(fā)失效。
我們將這種特殊的 Subjects 稱作 RxRelay
:
PublishRelay 就是 PublishSubject 去掉終止事件 onError
或 onCompleted
兼雄。
BehaviorRelay 就是 BehaviorSubject 去掉終止事件 onError
或 onCompleted
吟逝。