目录:
RxSwift - 入门
RxSwift Observable Create
RxSwift Subject
RxSwift Combination Operators
RxSwift Transforming Operators
RxSwift Filterning and conditional operators
RxSwift Mathematical and aggregate operators
RxSwift Connectable Operators
RxSwift ErrorHandding Operators
RxSwift Debug Operators
Demo地址
toArray
把事件包装成为一个数组,一次性发送出来
example("toArray") {
let disposeBag = DisposeBag()
Observable.range(start: 1, count: 10)
.toArray()
.subscribe { print($0) }
.disposed(by: disposeBag)
}
reduce
设置一个初始值,让后遍历事件序列中的每一个事件,根据closure计算值,把最终的结果发送出来
example("reduce") {
let disposeBag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.reduce(1, accumulator: +)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
concat
把多个Observable串联在一起,只有前面的的Observable调用了
completed
事件,后面的Observable发送的事件才会被监听到,如果当前事件发生了error
或者dispose
事件,整个Observable都会销毁
example("concat") {
let disposeBag = DisposeBag()
let subject1 = BehaviorSubject(value: "1")
let subject2 = BehaviorSubject(value: "a")
let subject = PublishSubject>()
subject.concat()
.subscribe(
onNext: { print("next ", $0) },
onError: { print("error ",$0) },
onCompleted: { print("onCompleted") },
onDisposed: { print("onDisposed") }
)
.disposed(by: disposeBag)
subject.onNext(subject1)
subject.onNext(subject2)
subject1.onNext("2")
subject1.onNext("3")
//subject2.onError(TestError.test)
//subject1.onNext("")
subject2.onNext("I would be ignored")
subject2.onNext("b")
subject1.onCompleted()
//subject1.dispose()
//subject1.onError(TestError.test)
subject2.onNext("c")
//subject2.onError(TestError.test)
subject2.onNext("d")
}