Content
- Basic
- Subject
- Combination Operators
- Transforming Operators
- Filtering and Conditional Operators
- Mathematical and Aggregate Operators
- Connectable Operators
- Error Handling Operators
- Debugging Operators
- Refer
Basic
- Observable
Observable 。 Observable
是观察者模式中被观察的对象,相当于一个事件序列 (GeneratorType) ,会向订阅者发送新产生的事件信息。事件信息分为三种:
- .Next(value) 表示新的事件数据。
- .Completed 表示事件序列的完结。
- .Error 同样表示完结,但是代表异常导致的完结。
|
|
- Subscribe
subscribe 就是一种简便的订阅信号的方法。这里的subscribe函数就是把消息发给观察者。
|
|
- empty
empty 是一个空的序列,它只发送 .Completed 消息。
|
|
- never
never 是没有任何元素、也不会发送任何事件的空序列。
|
|
- just
just 是只包含一个元素的序列,它会先发送 .Next(value) ,然后发送 .Completed 。
|
|
- of
of 可以把一系列元素转换成事件序列。
|
|
- from
from 是把 Swift 中的序列 (SequenceType) 转换成事件序列。 Creates an Observable sequence from a Sequence, such as an Array, Dictionary, or Set.
|
|
- create
create 可以通过闭包创建序列,通过 .on(e: Event) 添加事件
More info
|
|
- Range
Creates an Observable sequence that emits a range of sequential integers and then terminates
More info
|
|
- repeatElement
重复元素
More info
|
|
- generate
Creates an Observable sequence that generates values for as long as the provided condition evaluates to true
|
|
- deferred
Creates a new Observable sequence for each subscriber
More info
|
|
- error
Creates an Observable sequence that emits no items and immediately terminates with an error.
|
|
- doOn
Invokes a side-effect action for each emitted event and returns (passes through) the original event
More info
|
|
Subject
|
|
- PublishSubject
Broadcasts new events to all observers as of their time of the subscription.
|
|
- ReplaySubject
Broadcasts new events to all subscribers, and the specified bufferSize number of previous events to new subscribers.
|
|
- BehaviorSubject
Broadcasts new events to all subscribers, and the most recent (or initial) value to new subscribers.
|
|
- Variable
Wraps a BehaviorSubject, so it will emit the most recent (or initial) value to new subscribers. And Variable also maintains current value state. Variable will never emit an Error event. However, it will automatically emit a Completed event and terminate on deinit.
|
|
Combination Operators
- startWith
startWith 会在队列开始之前插入一个事件元素。
|
|
- merge
merge 就是 merge 啦,把两个队列按照顺序组合在一起。
More info
|
|
- zip
zip 人如其名,就是合并两条队列用的,不过它会等到两个队列的元素一一对应地凑齐了之后再合并
More info
|
|
- combineLatest
如果存在两条事件队列,需要同时监听,那么每当有新的事件发生的时候,combineLatest 会将每个队列的最新的一个元素进行合并
More info
|
|
- switchLatest
当你的事件序列是一个事件序列的序列 (Observable
>) 的时候,(可以理解成二维序列?),可以使用 switch 将序列的序列平铺成一维,并且在出现新的序列的时候,自动切换到最新的那个序列上。和 merge 相似的是,它也是起到了将多个序列『拍平』成一条序列的作用。
Transforms the elements emitted by an Observable sequence into Observable sequences, and emits elements from the most recent inner Observable sequence
|
|
Transforming Operators
- map
就是对每个元素都用函数做一次转换,挨个映射一遍
|
|
- flatMap
|
|
- scan
scan 它会把每次的运算结果累积起来,作为下一次运算的输入值。
More info
|
|
Filtering and Conditional Operators
- filter
filter 只会让符合条件的元素通过。
More info
|
|
- distinctUntilChanged
会过滤掉重复的事件More info
|
|
- elementAt
指定index位置元素的事件
More info
|
|
- single
Emits only the first element (or the first element that meets a condition) emitted by an Observable sequence. Will throw an error if the Observable sequence does not emit exactly one element.
|
|
- take
take 只获取序列中的前 n 个事件,在满足数量之后会自动 .Completed
More info
|
|
- takeLast
takeLast 只获取序列中的后 n 个事件
More info
|
|
- takeWhile
Emits elements from the beginning of an Observable sequence as long as the specified condition evaluates to true
More info
|
|
- takeUntil
直到 More info
|
|
- skip
跳过 More info
|
|
- skipWhile
条件跳过
More info
|
|
- skipWhileWithIndex
带index条件的跳过
|
|
- skipUntil
跳过, 直到…
More info
|
|
Mathematical and Aggregate Operators
- toArray
Converts an Observable sequence into an array, emits that array as a new single-element Observable sequence, and then terminates
More info
|
|
- reduce
Begins with an initial seed value, and then applies an accumulator closure to all elements emitted by an Observable sequence, and returns the aggregate result as a single-element Observable sequence
More info
|
|
- concat
concat 可以把多个事件序列合并起来。
More info
|
|
Connectable Operators
without connectable operators
|
|
- publish
将事件源转换成可以连接的序列
Converts the source Observable sequence into a connectable sequence
|
|
- replay
Converts the source Observable sequence into a connectable sequence, and will replay bufferSize number of previous emissions to each new subscriber
|
|
- multicast
Converts the source Observable sequence into a connectable sequence, and broadcasts its emissions via the specified subject.
|
|
|
|
Error Handling Operators
- catchErrorJustReturn
Recovers from an Error event by returning an Observable sequence that emits a single element and then terminates
More info
|
|
- catchError
Recovers from an Error event by switching to the provided recovery Observable sequence
More info
|
|
- retry
Recovers repeatedly Error events by resubscribing to the Observable sequence, indefinitely.
More info
|
|
- retry(_:)
Recovers repeatedly from Error events by resubscribing to the Observable sequence, up to maxAttemptCount number of retries.
More info
|
|
Debugging Operators
- debug
Prints out all subscriptions, events, and disposals.
|
|
- RxSwift.Resources.total
Provides a count of all Rx resource allocations, which is useful for detecting leaks during development
|
|