基础概念
- 一直在强调Observable Sequence
- 最简单的创建Sequence的方法
- Disposable:销毁时,可以在其中释放资源
_ = Observable<String>.create { observerOfString -> Disposable in
print("This will never be printed")
observerOfString.on(.next("😬"))
observerOfString.on(.completed)
return Disposables.create()
}
使用Observable创建和订阅Sequence
- 默认提供和一些创建Sequence的方法:create、never、empty、just、of、from、repeatElement、generate、deferred、error、doOn、
- 具体概念请到 Rx.playground -> Creating_and_Subscribing_to_Observables中查看
使用Subject创建Sequence
- PublishSubject observers在订阅该类型信号开始,接受event
- ReplaySubject 创建时有个参数叫bufferSize,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 对Subject的包装,使用.value赋值,相当于onNext
合并操作Combination Operators
- 提供用一些方法startWith、merge、zip、combineLatest、switchLatest
- 把一些单个数据合并到一起成为一个Observable信号
传输操作Transforming Operators
- 提供用一些方法map、flatMap、flatMapLatest、scan
过滤操作Filtering and Conditional Operators
- 提供用一些方法filter、distinctUntilChanged、elementAt、single、take、takeLatest、takeWhile、takeUntil、skip、skipWhile、skipWhileWithIndex、skipUntil
集合操作Mathematical and Aggregate Operators
- toArray
Observable.range(start: 1, count: 10)
.toArray()
.subscribe { print($0) }
.addDisposableTo(disposeBag)
- reduce
Observable.of(10, 100, 1000)
.reduce(1, accumulator: +) // 求+运算,1+10+100+1000
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
- contact
let disposeBag = DisposeBag()
let subject1 = BehaviorSubject(value: "🍎")
let subject2 = BehaviorSubject(value: "🐶")
let variable = Variable(subject1)
variable.asObservable()
.concat()
.subscribe { print($0) }
.addDisposableTo(disposeBag)
subject1.onNext("🍐")
subject1.onNext("🍊")
variable.value = subject2
subject2.onNext("I would be ignored")
subject2.onNext("🐱")
subject1.onCompleted()
subject2.onNext("🐭")
// 在subject1调用onCompleted()之前,subject2都收不到信号
可连接的操作Connectable Operators
- publish Converts the source Observable sequence into a connectable sequence. 将一般的�Observable Sequence转换为connectable sequence
- replay,新订阅开始前时,会收到bufferSize个之前的信号
- multicast,可以用一个interval携带多个observable sequence
错误处理机制Error Handling Operators
- catchErrorJustReturn("xxx") 不管返回什么错误都只传给订阅者一个"xxx"
- catchError 抓取到异常后,Recovers一个已有的Sequence
- retry(maxAttemptCount: Int) 得到错误返回后重试,maxAttemptCount为最大重试次数
调试Debugging Operators
- debug Prints out all subscriptions, events, and disposals.
Enable RxSwift.resourceCount
Follow these instructions to enable RxSwift.Resources.total
in your project:
- CocoaPods
- Add a
post_install
hook to your Podfile, e.g.:
target 'AppTarget' do
pod 'RxSwift'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'RxSwift'
target.build_configurations.each do |config|
if config.name == 'Debug'
config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['-D', 'TRACE_RESOURCES']
end
end
end
end
end
- Run
pod update
. - Build project (Product → Build).
- Carthage
- Run
carthage build --configuration Debug
. - Build project (Product → Build).