Rxswift Tutorial

1. Data sequence

Observable represents all Changes to your data as an event sequence that go through time.

屏幕快照 2020-05-17 下午7.13.10.png

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

屏幕快照 2020-05-17 下午7.24.31.png
  • here is an example for UISwitch

When we toggle this Switch, it emits the bool value of (True or False)

屏幕快照 2020-05-17 下午7.26.27.png

3. Operators

You can use different methods on these Observable class. similar with the methods that you have on any sequence.

屏幕快照 2020-05-17 下午7.39.33.png
  • 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

.png
Screenshot 2020-08-24 at 5.27.52 PM.png
Screenshot 2020-08-24 at 5.23.54 PM.png
Screenshot 2020-08-24 at 5.17.50 PM.png
Screenshot 2020-08-24 at 5.17.22 PM.png

RxRelay

RxRelay 既是 可监听序列 也是 观察者。

他和 Subjects 相似,唯一的区别是不会接受 onError 或 onCompleted 这样的终止事件。

在将非 Rx 样式的 API 转化为 Rx 样式时,Subjects 是非常好用的。不过一旦 Subjects 接收到了终止事件 onError 或 onCompleted。他就无法继续工作了,也不会转发后续任何事件。有些时候这是合理的,但在多数场景中这并不符合我们的预期。

在这些场景中一个更严谨的做法就是,创造一种特殊的 Subjects,这种 Subjects 不会接受终止事件。有了他,我们将 API 转化为 Rx 样式时,就不必担心一个意外的终止事件,导致后续事件转发失效。

我们将这种特殊的 Subjects 称作 RxRelay

PublishRelay 就是 PublishSubject 去掉终止事件 onErroronCompleted

BehaviorRelay 就是 BehaviorSubject 去掉终止事件 onErroronCompleted

你可能感兴趣的:(Rxswift Tutorial)