RxSwift官方使用示例之<可連線操作符>
本文是示例主要來自於官方的例項程式碼,可從這裡下載 ,例項程式碼稍有改動。其中的圖片來自於reactivex.io/
本文主要的作用是檢視例項程式碼並檢視對應例項程式碼的效果,不需要執行專案
操作符目錄
- RxSwift官方使用示例之<建立和訂閱Observables>
- RxSwift官方使用示例之<使用Subjects>
- RxSwift官方使用示例之<組合操作符>
- RxSwift官方使用示例之<轉換操作符>
- RxSwift官方使用示例之<過濾及條件操作符>
- RxSwift官方使用示例之<數學和聚合操作符>
- RxSwift官方使用示例之<可連線操作符>
- RxSwift官方使用示例之<錯誤處理操作符>
debug 操作符會列印所有的 subscriptions, events, disposals
公用程式碼,之後的程式碼中會用到
public func delay(_ delay: Double, closure: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
closure()
}
}
interval
建立一個每隔一段時間傳送元素的Observable
序列
let interval = Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
_ = interval
.subscribe(onNext: { print("Subscription: 1, Event: \($0)") })
delay(2) {
_ = interval
.subscribe(onNext: { print("Subscription: 2, Event: \($0)") })
}
列印
Subscription: 1, Event: 0
Subscription: 1, Event: 1
Subscription: 1, Event: 2
Subscription: 2, Event: 0
Subscription: 1, Event: 3
Subscription: 2, Event: 1
Subscription: 1, Event: 4
注意第2個訂閱者收到事件是在第2秒,並且是從0開始計數
publish
把源Observable
序列轉換成一個可連線(contactable)序列
有圖可知,
publish
的序列在connect
之後,會對訂閱者傳送資料,並且是傳送訂閱後序列產生的新資料
``` let intSequence = Observable.interval(.seconds(1), scheduler: MainScheduler.instance) .publish() // 轉為可連線序列
_ = intSequence .subscribe(onNext: { print("Subscription 1:, Event: ($0)") })
delay(2) { _ = intSequence.connect() } // 開始連線,連線之後下一秒開始對已訂閱者 Subscription 1 傳送資料
delay(4) { _ = intSequence .subscribe(onNext: { print("Subscription 2:, Event: ($0)") }) }
delay(6) { _ = intSequence .subscribe(onNext: { print("Subscription 3:, Event: ($0)") }) } ``` 列印
Subscription 1:, Event: 0
Subscription 1:, Event: 1
Subscription 2:, Event: 1
Subscription 1:, Event: 2
Subscription 2:, Event: 2
Subscription 1:, Event: 3
Subscription 2:, Event: 3
Subscription 3:, Event: 3
Subscription 1:, Event: 4
Subscription 2:, Event: 4
Subscription 3:, Event: 4
...
replay
將源 Observable
序列轉換為可連線序列,並將重發之前傳送的 bufferSize 數量的元素給每個新訂閱者
```
let intSequence = Observable
_ = intSequence .subscribe(onNext: { print("Subscription 1:, Event: ($0)") })
delay(2) { _ = intSequence.connect() } // 開始連線,下一秒開始傳送資料
delay(4) { _ = intSequence .subscribe(onNext: { print("Subscription 2:, Event: ($0)") }) }
delay(8) {
_ = intSequence
.subscribe(onNext: { print("Subscription 3:, Event: ($0)") })
}
``
列印(
// 之後是分析`)
Subscription 1:, Event: 0 // 第3秒列印
Subscription 2:, Event: 0 // 第4秒列印,先把快取的資料0給新訂閱者 Subscription 2
Subscription 1:, Event: 1 // 依次給訂閱者1、2傳送資料1
Subscription 2:, Event: 1
Subscription 1:, Event: 2 // 第5秒列印,給訂閱者1、2傳送資料2
Subscription 2:, Event: 2
Subscription 1:, Event: 3 // 第6秒列印,給訂閱者1、2傳送資料3
Subscription 2:, Event: 3
Subscription 1:, Event: 4 // 第7秒列印,給訂閱者1、2傳送資料4
Subscription 2:, Event: 4
Subscription 3:, Event: 0 // 第8秒列印,先把快取的資料 01234 給新訂閱者 Subscription 3
Subscription 3:, Event: 1
Subscription 3:, Event: 2
Subscription 3:, Event: 3
Subscription 3:, Event: 4
Subscription 1:, Event: 5 // 然後再給訂閱者1、2、3傳送資料5
Subscription 2:, Event: 5
Subscription 3:, Event: 5
Subscription 1:, Event: 6 // 第9秒列印,給訂閱者1、2、3傳送資料6
Subscription 2:, Event: 6
Subscription 3:, Event: 6
...
multicast
將源 Observable
序列轉換為可連線序列,並通過指定訂閱者廣播資料
```
let subject = PublishSubject
_ = subject .subscribe(onNext: { print("Subject: ($0)") })
let intSequence = Observable
_ = intSequence .subscribe(onNext: { print("\tSubscription 1:, Event: ($0)") })
delay(2) { _ = intSequence.connect() }
delay(4) { _ = intSequence .subscribe(onNext: { print("\tSubscription 2:, Event: ($0)") }) }
delay(6) { _ = intSequence .subscribe(onNext: { print("\tSubscription 3:, Event: ($0)") }) } ``` 列印
Subject: 0
Subscription 1:, Event: 0
Subject: 1
Subscription 1:, Event: 1
Subscription 2:, Event: 1
Subject: 2
Subscription 1:, Event: 2
Subscription 2:, Event: 2
Subject: 3
Subscription 1:, Event: 3
Subscription 2:, Event: 3
Subscription 3:, Event: 3
...