(部分翻译和注解来自 ReactiveX文档中文翻译)
Introduction
为什么使用 RxSwift?
我们写的很多代码实际上是为了解决和响应外部事件。当用户操作一个控件的时候,我们需要使用 @IBAction 来响应事件。我们需要观察通知来检测键盘改变位置。当 URL Sessions 带着响应的数据返回时,我们需要提供闭包来执行我们的操作。我们还需要使用 KVO 来检测变量的值改变。这些大量的编写机制使得我们的代码结构变的更加复杂。如果有一种统一的编写机制来完成所有的这些调用/响应代码是不是更棒呢?Rx 就是为解决这些问题而生的。
Observable
理解 RxSwift 的关键是理解 Observable 的概念。要理解它的创建,操作以及为了对变化做出响应操作而进行的订阅(subscribe)。
创建和订阅 Observable
要理解本框架,第一步需要理解如何创建 Observable。有很多函数可以创建 Observable。
创建 Observable 之后,如果没有订阅者订阅该 observable,那么什么事情也不会发生,所以我们将同时解释创建和订阅。
empty
empty@H_301_29@ 创建一个空的序列。它仅发送
.Completed@H_301_29@ 消息。
运行结果:
@H_403_33@--- empty example --- Completednever
never@H_301_29@ 创建一个序列,该序列永远不会发送消息,
.Completed@H_301_29@ 消息也不会发送。
运行结果:
@H_403_33@--- never example ---just
just@H_301_29@ 代表只包含一个元素的序列。它将向订阅者发送两个消息,第一个消息是其中元素的值,另一个是
.Completed@H_301_29@。
运行结果:
@H_403_33@--- just example --- Next(32) CompletedsequenceOf
sequenceOf@H_301_29@ 通过固定数目的元素创建一个序列
运行结果:
@H_403_33@--- sequenceOf example --- Next(0) Next(1) Next(2) Next(3) Next(4) Next(5) Next(6) Next(7) Next(8) Next(9) CompletedtoObservable
toObservable@H_301_29@ 在一个数组的基础上创建一个序列
运行结果:
@H_403_33@--- toObservable example --- Next(1) Next(2) Next(3) Next(4) Next(5) Completedcreate
create@H_301_29@ 使用 Swift 闭包来创建一个序列。该例子中,创建了
just@H_301_29@ 操作符的自定义版本。
运行结果:
@H_403_33@--- create example --- Next(5) Completedgenerate
generate@H_301_29@ 创建的序列可以自己生成它的值,并且在之前值的基础上来判断什么时候结束。
运行结果:
@H_403_33@--- generate example --- Next(0) Next(1) Next(2) Completederror
创建一个不发送任何 item 的 Observable,以 error 中指
@H_403_33@example("error") { let error = NSError(domain: "Test",code: -1,userInfo: nil) let erroredSequence = Observable<Int>.error(error) let subscription = erroredSequence .subscribe { event in print(event) } }运行结果:
@H_403_33@--- error example --- Error(Error Domain=Test Code=-1 "(null)")deferred
直到 observer 订阅之后才创建 Observable,并且为每一个 observer 创建一个全新的 Observable
do not create the Observable until the observer subscribes,and create a fresh Observable for each observer
运行结果:
@H_403_33@--- deferred example --- creating emmiting Next(0) Next(1) Next(2) creating emmiting Next(0) Next(1) Next(2)在 RxCocoa 库中还有很多其他非常有用的方法,例如:
rx_observe@H_301_29@ 存在于所有 NSObject 子类中,封装了 KVO
rx_tap@H_301_29@ 存在于 button 中,封装了 @IBActions
rx_notification@H_301_29@ 封装了 NotificationCenter
- …
Subjects
Subject 可以看成是一个桥梁或者代理,在某些ReactiveX实现中,它同时充当了 Observer 和 Observable 的角色。因为它是一个Observer,它可以订阅一个或多个 Observable;又因为它是一个 Observable,它可以转发它收到(Observe)的数据,也可以发射新的数据。
辅助函数:
@H_403_33@func writeSequenceToConsole<O: ObservableType>(name: String,sequence: O) -> Disposable { return sequence .subscribe { e in print("Subscription: \(name),event: \(e)") } }PublishSubject
PublishSubject@H_301_29@ 只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者。
运行结果:
@H_403_33@--- PublishSubject example --- Subscription: 1,event: Next(a) Subscription: 1,event: Next(b) Subscription: 1,event: Next(c) Subscription: 2,event: Next(c) Subscription: 1,event: Next(d) Subscription: 2,event: Next(d)ReplaySubject
ReplaySubject@H_301_29@ 会发射所有来自原始Observable的数据给观察者,无论它们是何时订阅的。当一个新的 observer 订阅了一个
ReplaySubject@H_301_29@ 之后,他将会收到当前缓存在 buffer 中的数据和这之后产生的新数据。在下面的例子中,缓存大小为
1@H_301_29@ 所以 observer 将最多能够收到订阅时间点之前的一个数据。例如,
Subscription: 2@H_301_29@ 能够收到消息
"b"@H_301_29@,而这个消息是在他订阅之前发送的,但是没有办法收到消息
"a"@H_301_29@ 因为缓存的容量小于
2@H_301_29@。
运行结果:
@H_403_33@--- ReplaySubject example --- Subscription: 1,event: Next(a) Subscription: 1,event: Next(b) Subscription: 2,event: Next(b) Subscription: 1,event: Next(c) Subscription: 2,event: Next(c) Subscription: 1,event: Next(d) Subscription: 2,event: Next(d)BehaviorSubject
当观察者订阅 BehaviorSubject@H_301_29@ 时,它开始发射原始 Observable 最近发射的数据(如果此时还没有收到任何数据,它会发射一个默认值),然后继续发射其它任何来自原始Observable的数据。
运行结果:
@H_403_33@--- BehaviorSubject example --- Subscription: 1,event: Next(z) Subscription: 1,event: Next(b) Subscription: 2,event: Next(d) Subscription: 1,event: Completed Subscription: 2,event: CompletedVariable
Variable@H_301_29@ 封装了
BehaviorSubject@H_301_29@。使用 variable 的好处是 variable 将不会显式的发送
Error@H_301_29@ 或者
Completed@H_301_29@。在 deallocated 的时候,
Variable@H_301_29@ 会自动的发送 complete 事件。
运行结果:
@H_403_33@--- Variable example --- Subscription: 1,event: Completed变换操作
下面列出了可用于对 Observable 发射的数据执行变换操作的各种操作符。
map@H_301_29@ / select@H_301_29@
select@H_301_29@
对序列的每一项都应用一个函数来变换 Observable 发射的数据序列
@H_403_33@example("map") { let originalSequence = Observable.of(1,3) _ = originalSequence .map { number in number * 2 } .subscribe { print($0) } }运行结果:
@H_403_33@--- map example --- Next(2) Next(4) Next(6) CompletedflatMap@H_301_29@
将每个 Obserable 发射的数据变换为 Observable 的集合,然后将其 “拍扁”(降维 flatten)成一个 Observable。
@H_403_33@example("flatMap") { let sequenceInt = Observable.of(1,3) let sequenceString = Observable.of("A","B","C","D","E","F","--") _ = sequenceInt .flatMap { (x:Int) -> Observable<String> in print("from sequenceInt \(x)") return sequenceString } .subscribe { print($0) } }运行结果:
@H_403_33@--- flatMap example --- from sequenceInt 1 Next(A) Next(B) Next(C) Next(D) Next(E) Next(F) Next(--) from sequenceInt 2 Next(A) Next(B) Next(C) Next(D) Next(E) Next(F) Next(--) from sequenceInt 3 Next(A) Next(B) Next(C) Next(D) Next(E) Next(F) Next(--) Completedscan@H_301_29@
对 Observable 发射的每一项数据应用一个函数,然后按顺序依次发射每一个值
@H_403_33@example("scan") { let sequenceToSum = Observable.of(0,5) _ = sequenceToSum .scan(0) { acum,elem in acum + elem } .subscribe { print($0) } }运行结果:
@H_403_33@--- scan example --- Next(0) Next(1) Next(3) Next(6) Next(10) Next(15) Completed过滤操作
从源 Observable 中选择特定的数据发送
filter@H_301_29@
只发送 Observable 中通过特定测试的数据
@H_403_33@example("filter") { let subscription = Observable.of(0,9) .filter { $0 % 2 == 0 } .subscribe { print($0) } }运行结果:
@H_403_33@--- filter example --- Next(0) Next(2) Next(4) Next(6) Next(8) CompleteddistinctUntilChanged@H_301_29@
过滤掉连续重复的数据
@H_403_33@example("distinctUntilChanged") { let subscription = Observable.of(1,4) .distinctUntilChanged() .subscribe { print($0) } }运行结果:
@H_403_33@--- distinctUntilChanged example --- Next(1) Next(2) Next(3) Next(1) Next(4) Completedtake@H_301_29@
仅发送 Observable 的前 n 个数据项
@H_403_33@example("take") { let subscription = Observable.of(1,6) .take(3) .subscribe { print($0) } }运行结果:
@H_403_33@--- take example --- Next(1) Next(2) Next(3) Completed结合操作(Combination operators)
将多个 Observable 结合成一个 Observable
startWith@H_301_29@
在数据序列的开头增加一些数据
@H_403_33@example("startWith") { let subscription = Observable.of(4,9) .startWith(3) .startWith(2) .startWith(1) .startWith(0) .subscribe { print($0) } }运行结果:
@H_403_33@--- startWith example --- Next(0) Next(1) Next(2) Next(3) Next(4) Next(5) Next(6) Next(7) Next(8) Next(9) CompletedcombineLatest@H_301_29@
当两个 Observables 中的任何一个发射了一个数据时,通过一个指定的函数组合每个Observable发射的最新数据(一共两个数据),然后发射这个函数的结果
@H_403_33@example("combineLatest 1") { let intOb1 = PublishSubject<String>() let intOb2 = PublishSubject<Int>() _ = Observable.combineLatest(intOb1,intOb2) { "\($0) \($1)" } .subscribe { print($0) } intOb1.on(.Next("A")) intOb2.on(.Next(1)) intOb1.on(.Next("B")) intOb2.on(.Next(2)) }运行结果:
@H_403_33@--- combineLatest 1 example --- Next(A 1) Next(B 1) Next(B 2)为了能够产生结果,两个序列中都必须保证至少有一个元素
@H_403_33@example("combineLatest 2") { let intOb1 = Observable.just(2) let intOb2 = Observable.of(0,4) _ = Observable.combineLatest(intOb1,intOb2) { $0 * $1 } .subscribe { print($0) } }运行结果:
@H_403_33@--- combineLatest 2 example --- Next(0) Next(2) Next(4) Next(6) Next(8) CompletedCombine latest 有超过 2 个参数的版本
@H_403_33@example("combineLatest 3") { let intOb1 = Observable.just(2) let intOb2 = Observable.of(0,3) let intOb3 = Observable.of(0,intOb2,intOb3) { ($0 + $1) * $2 } .subscribe { print($0) } }运行结果:
@H_403_33@--- combineLatest 3 example --- Next(0) Next(5) Next(10) Next(15) Next(20) CompletedCombinelatest 可以作用于不同数据类型的序列
@H_403_33@example("combineLatest 4") { let intOb = Observable.just(2) let stringOb = Observable.just("a") _ = Observable.combineLatest(intOb,stringOb) { "\($0) " + $1 } .subscribe { print($0) } }运行结果:
@H_403_33@--- combineLatest 4 example --- Next(2 a) CompletedcombineLatest@H_301_29@ 方法可以在 Array 上使用,数组元素类型必须遵循
ObservableType@H_301_29@ 协议
数组中的元素类型必须为 Observables@H_301_29@
withLatestFrom@H_301_29@
Merges two observable sequences into one observable sequence by using latest element from the second sequence every time when self emitts an element.
将两个 Observable 序列合并为一个。每当 self 队列发射一个元素时,从第二个序列中取出最新的一个值。
@H_403_33@example("withLatestFrom") { let subjectA = PublishSubject<String>() let subjectB = PublishSubject<String>() subjectA .withLatestFrom(subjectB) .subscribeNext({ (data) in print(data) }) subjectA.onNext("a1") subjectB.onNext("b1") subjectA.onNext("a2") subjectA.onNext("a3") subjectB.onNext("b2") subjectA.onNext("a4") }运行结果:
@H_403_33@--- withLatestFrom example --- b1 b1 b2zip@H_301_29@
使用一个函数组合多个Observable发射的数据集合,然后再发射这个结果(从序列中依次取数据)
@H_403_33@example("zip 1") { let intOb1 = PublishSubject<String>() let intOb2 = PublishSubject<Int>() _ = Observable.zip(intOb1,intOb2) { "\($0) \($1)" } .subscribe { print($0) } intOb1.on(.Next("A")) intOb2.on(.Next(1)) intOb1.on(.Next("B")) intOb1.on(.Next("C")) intOb2.on(.Next(2)) }运行结果:
@H_403_33@--- zip 1 example --- Next(A 1) Next(B 2) @H_403_33@example("zip 2") { let intOb1 = Observable.just(2) let intOb2 = Observable.of(0,4) _ = Observable.zip(intOb1,intOb2) { $0 * $1 } .subscribe { print($0) } }运行结果:
@H_403_33@--- zip 2 example --- Next(0) Completed @H_403_33@example("zip 3") { let intOb1 = Observable.of(0,1) let intOb2 = Observable.of(0,intOb3) { ($0 + $1) * $2 } .subscribe { print($0) } }运行结果:
@H_403_33@--- zip 3 example --- Next(0) Next(2) Completedmerge@H_301_29@
合并多个 Observables 的组合成一个
@H_403_33@example("merge 1") { let subject1 = PublishSubject<Int>() let subject2 = PublishSubject<Int>() _ = Observable.of(subject1,subject2) .merge() .subscribeNext { int in print(int) } subject1.on(.Next(20)) subject1.on(.Next(40)) subject1.on(.Next(60)) subject2.on(.Next(1)) subject1.on(.Next(80)) subject1.on(.Next(100)) subject2.on(.Next(1)) }运行结果:
@H_403_33@--- merge 1 example --- 20 40 60 1 80 100 1 @H_403_33@example("merge 2") { let subject1 = PublishSubject<Int>() let subject2 = PublishSubject<Int>() _ = Observable.of(subject1,subject2) .merge(maxConcurrent: 2) .subscribe { print($0) } subject1.on(.Next(20)) subject1.on(.Next(40)) subject1.on(.Next(60)) subject2.on(.Next(1)) subject1.on(.Next(80)) subject1.on(.Next(100)) subject2.on(.Next(1)) }运行结果:
@H_403_33@--- merge 2 example --- Next(20) Next(40) Next(60) Next(1) Next(80) Next(100) Next(1)switchLatest@H_301_29@
将一个发射多个 Observables 的 Observable 转换成另一个单独的 Observable,后者发射那些 Observables 最近发射的数据项
Switch 订阅一个发射多个 Observables 的 Observable。它每次观察那些 Observables 中的一个,Switch 返回的这个Observable取消订阅前一个发射数据的 Observable,开始发射最近的Observable 发射的数据。注意:当原始 Observable 发射了一个新的 Observable 时(不是这个新的 Observable 发射了一条数据时),它将取消订阅之前的那个 Observable。这意味着,在后来那个 Observable 产生之后到它开始发射数据之前的这段时间里,前一个 Observable 发射的数据将被丢弃
@H_403_33@example("switchLatest") { let var1 = Variable(0) let var2 = Variable(200) // var3 is like an Observable<Observable<Int>> let var3 = Variable(var1.asObservable()) let d = var3 .asObservable() .switchLatest() .subscribe { print($0) } var1.value = 1 var1.value = 2 var1.value = 3 var1.value = 4 var3.value = var2.asObservable() var2.value = 201 var1.value = 5 var1.value = 6 var1.value = 7 }运行结果:
@H_403_33@--- switchLatest example --- Next(0) Next(1) Next(2) Next(3) Next(4) Next(200) Next(201) CompletedError Handling Operators
下面的操作符帮助我们从 Observable 发射的 error 通知做出响应或者从错误中恢复。
catchError@H_301_29@
收到 Error@H_301_29@ 通知之后,转而发送一个没有错误的序列。
运行结果:
@H_403_33@--- catchError 1 example --- Next(1) Next(2) Next(3) Next(4) Next(100) Next(200) Next(300) Next(400) Completed @H_403_33@example("catchError 2") { let sequenceThatFails = PublishSubject<Int>() _ = sequenceThatFails .catchErrorJustReturn(100) .subscribe { print($0) } sequenceThatFails.on(.Next(1)) sequenceThatFails.on(.Next(2)) sequenceThatFails.on(.Next(3)) sequenceThatFails.on(.Next(4)) sequenceThatFails.on(.Error(NSError(domain: "Test",userInfo: nil))) }运行结果:
@H_403_33@--- catchError 2 example --- Next(1) Next(2) Next(3) Next(4) Next(100) Completedretry@H_301_29@
如果原始 Observable 遇到错误,重新订阅,心里默念,不会出错不会出错…
@H_403_33@example("retry") { var count = 1 // bad practice,only for example purposes let funnyLookingSequence = Observable<Int>.create { observer in let error = NSError(domain: "Test",userInfo: nil) observer.on(.Next(0)) observer.on(.Next(1)) observer.on(.Next(2)) if count < 2 { observer.on(.Error(error)) count += 1 } observer.on(.Next(3)) observer.on(.Next(4)) observer.on(.Next(5)) observer.on(.Completed) return NopDisposable.instance } _ = funnyLookingSequence .retry() .subscribe { print($0) } }运行结果:
@H_403_33@--- retry example --- Next(0) Next(1) Next(2) Next(0) Next(1) Next(2) Next(3) Next(4) Next(5) CompletedObservable Utility Operators
下面的操作符可以当做一个工具集,方便操作 Observable
subscribe@H_301_29@
@H_403_33@example("subscribe") { let sequenceOfInts = PublishSubject<Int>() _ = sequenceOfInts .subscribe { print($0) } sequenceOfInts.on(.Next(1)) sequenceOfInts.on(.Completed) }
运行结果:
@H_403_33@--- subscribe example --- Next(1) Completed下面是几个 subscribe@H_301_29@ 操作符的变体
subscribeNext@H_301_29@
@H_403_33@example("subscribeNext") { let sequenceOfInts = PublishSubject<Int>() _ = sequenceOfInts .subscribeNext { print($0) } sequenceOfInts.on(.Next(1)) sequenceOfInts.on(.Completed) }
运行结果:
@H_403_33@--- subscribeNext example --- 1subscribeCompleted@H_301_29@
@H_403_33@example("subscribeCompleted") { let sequenceOfInts = PublishSubject<Int>() _ = sequenceOfInts .subscribeCompleted { print("It's completed") } sequenceOfInts.on(.Next(1)) sequenceOfInts.on(.Completed) }
运行结果:
@H_403_33@--- subscribeCompleted example --- It's completedsubscribeError@H_301_29@
@H_403_33@example("subscribeError") {
let sequenceOfInts = PublishSubject<Int>()
_ = sequenceOfInts
.subscribeError { error in
print(error)
}
sequenceOfInts.on(.Next(1))
sequenceOfInts.on(.Error(NSError(domain: "Examples",userInfo: nil)))
}
运行结果:
@H_403_33@--- subscribeError example --- Error Domain=Examples Code=-1 "(null)"doOn@H_301_29@
注册一个操作来监听事件的生命周期
(register an action to take upon a variety of Observable lifecycle events)
运行结果:
@H_403_33@--- doOn example --- Intercepted event Next(1) Next(1) Intercepted event Completed Completed条件和布尔操作(Conditional and Boolean Operators)
下面的操作符可用于根据条件发射或变换 Observables,或者对它们做布尔运算:
takeUntil@H_301_29@
当第二个 Observable 发送数据之后,丢弃第一个 Observable 在这之后的所有消息。
@H_403_33@example("takeUntil") { let originalSequence = PublishSubject<Int>() let whenThisSendsNextWorldStops = PublishSubject<Int>() _ = originalSequence .takeUntil(whenThisSendsNextWorldStops) .subscribe { print($0) } originalSequence.on(.Next(1)) originalSequence.on(.Next(2)) originalSequence.on(.Next(3)) originalSequence.on(.Next(4)) whenThisSendsNextWorldStops.on(.Next(1)) originalSequence.on(.Next(5)) }运行结果:
@H_403_33@--- takeUntil example --- Next(1) Next(2) Next(3) Next(4) CompletedtakeWhile@H_301_29@
发送原始 Observable 的数据,直到一个特定的条件为 false
@H_403_33@example("takeWhile") { let sequence = PublishSubject<Int>() _ = sequence .takeWhile { int in int < 4 } .subscribe { print($0) } sequence.on(.Next(1)) sequence.on(.Next(2)) sequence.on(.Next(3)) sequence.on(.Next(4)) sequence.on(.Next(5)) }运行结果:
@H_403_33@--- takeWhile example --- Next(1) Next(2) Next(3) Completed算数和聚合(Mathematical and Aggregate Operators)
concat@H_301_29@
合并两个或者以上的 Observable 的消息,并且这些消息的发送时间不会交叉。(队列先后顺序不会交叉)
@H_403_33@example("concat") { let var1 = BehaviorSubject(value: 0) let var2 = BehaviorSubject(value: 200) // var3 is like an Observable<Observable<Int>> let var3 = BehaviorSubject(value: var1) let d = var3 .concat() .subscribe { print($0) } var1.on(.Next(1)) var1.on(.Next(2)) var1.on(.Next(3)) var1.on(.Next(4)) var3.on(.Next(var2)) var2.on(.Next(201)) var1.on(.Next(5)) var1.on(.Next(6)) var1.on(.Next(7)) var1.on(.Completed) var2.on(.Next(202)) var2.on(.Next(203)) var2.on(.Next(204)) }运行结果:
@H_403_33@--- concat example --- Next(0) Next(1) Next(2) Next(3) Next(4) Next(5) Next(6) Next(7) Next(201) Next(202) Next(203) Next(204)reduce@H_301_29@
按顺序对Observable发射的每项数据应用一个函数并发射最终的值。
Reduce@H_301_29@ 操作符对原始 Observable 发射数据的第一项应用一个函数,然后再将这个函数的返回值与第二项数据一起传递给函数,以此类推,持续这个过程知道原始Observable发射它的最后一项数据并终止,此时 Reduce 返回的 Observable 发射这个函数返回的最终值。与数组序列的
reduce@H_301_29@ 操作类似。
运行结果:
@H_403_33@--- reduce example --- Next(45) Completed