print("*****startWith*****") Observable.of("1", "2", "3", "4") .startWith("A") .startWith("B") .startWith("C", "a", "b") .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) //效果: CabBA1234
print("*****merge*****") let subject1 = PublishSubject<String>() let subject2 = PublishSubject<String>() // merge subject1和subject2 Observable.of(subject1, subject2) .merge() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) subject1.onNext("P") subject1.onNext("X") subject2.onNext("W") subject2.onNext("X") subject1.onNext("P") subject2.onNext("X") /// MARK: 任何一个响应都会勾起新序列响应
print("*****zip*****") let stringSubject = PublishSubject<String>() let intSubject = PublishSubject<Int>() Observable.zip(stringSubject, intSubject) { stringElement, intElement in "\(stringElement) \(intElement)" } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) stringSubject.onNext("P") stringSubject.onNext("X") // 到这里存储了 P X 但是不会响应除非;另一个响应 intSubject.onNext(1) // 勾出一个 intSubject.onNext(2) // 勾出另一个 stringSubject.onNext("i") // 存一个 intSubject.onNext(3) // 勾出一个 /// MARK: 只有两个序列同时有值的时候才会响应,否则存值
print("*****combineLatest*****") let stringSub = PublishSubject<String>() let intSub = PublishSubject<Int>() Observable.combineLatest(stringSub, intSub) { strElement, intElement in "\(strElement) \(intElement)" } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) stringSub.onNext("P") // 存一个 P stringSub.onNext("X") // 存了一个覆盖 - 和zip不一样 intSub.onNext(1) // 发现strOB也有X 响应 X 1 intSub.onNext(2) // 覆盖1 -> 2 发现strOB 有值X 响应 X 2 stringSub.onNext("PXWX") // 覆盖X -> PXWX 发现intOB 有值2 响应 PXWX 2 // combineLatest 比较zip 会覆盖 // 应用非常频繁: 比如账户和密码同时满足->才能登陆. 不关系账户密码怎么变化的只要查看最后有值就可以 loginEnable
print("*****switchLatest*****") let switchLatestSub1 = BehaviorSubject(value: "L") let switchLatestSub2 = BehaviorSubject(value: "1") let switchLatestSub = BehaviorSubject(value: switchLatestSub1)// 选择了 switchLatestSub1 就不会监听 switchLatestSub2 switchLatestSub.asObservable() .switchLatest() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) switchLatestSub1.onNext("G") switchLatestSub1.onNext("_") switchLatestSub2.onNext("2") switchLatestSub2.onNext("3") // 2-3都会不会监听,但是默认保存由 2覆盖1 3覆盖2 switchLatestSub.onNext(switchLatestSub2) // 切换到 switchLatestSub2 switchLatestSub1.onNext("*") switchLatestSub1.onNext("PXWX") // 原理同上面 下面如果再次切换到 switchLatestSub1会打印出 PXWX switchLatestSub2.onNext("4")
print("*****map*****") let ob = Observable.of(1,2,3,4) ob.map { (number) -> Int in return number+2 } .subscribe{ print("\($0)") } .disposed(by: disposeBag)
flatMap
和flatMapLatest
的区别是,flatMapLatest
只会从最近的内部可观测序列发射元素print("*****flatMap*****") let boy = PXPlayer(score: 100) let girl = PXPlayer(score: 90) let player = BehaviorSubject(value: boy) player.asObservable() .flatMap { $0.score.asObservable() } // 本身score就是序列 模型就是序列中的序列 .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) boy.score.onNext(60) player.onNext(girl) boy.score.onNext(50) boy.score.onNext(40)// 如果切换到 flatMapLatest 就不会打印 girl.score.onNext(10) girl.score.onNext(0)
flatMapLatest
实际上是map
和switchLatest
操作符的组合print("*****filter*****") Observable.of(1,2,3,4,5,6,7,8,9,0) .filter { $0 % 2 == 0 } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag)
print("*****distinctUntilChanged*****") Observable.of("1", "2", "2", "2", "3", "3", "4") .distinctUntilChanged() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag)
print("*****elementAt*****") Observable.of("P", "X", "W", "X") .elementAt(2) .subscribe(onNext: { print($0) }) .disposed(by: disposeBag)
print("*****single*****") Observable.of("PXWX", "KK") .single() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) Observable.of("PXWX", "KK") .single { $0 == "KK" } .subscribe { print($0) } .disposed(by: disposeBag)
print("*****take*****") Observable.of("Tony", "Jiaxiaochao","KK", "PXWX") .take(2) .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) // MARK: Tony Jiaxiaochao
print("*****takeLast*****") Observable.of("Tony", "Jiaxiaochao","KK", "PXWX") .takeLast(3) .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) // MARK: Jiaxiaochao KK PXWX
print("*****takeWhile*****") Observable.of(1, 2, 3, 4, 5, 6) .takeWhile { $0 < 3 } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) // MARK: 1 2
print("*****takeUntil*****") let sourceSequence = PublishSubject<String>() let referenceSequence = PublishSubject<String>() sourceSequence .takeUntil(referenceSequence) .subscribe { print($0) } .disposed(by: disposeBag) sourceSequence.onNext("PXWX") sourceSequence.onNext("Jiaxiaochao") sourceSequence.onNext("KK") referenceSequence.onNext("Tony") // 条件一出来,下面就走不了 sourceSequence.onNext("Lina") sourceSequence.onNext("Mike") sourceSequence.onNext("Jeak")
print("*****toArray*****") Observable.range(start: 1, count: 10) .toArray() .subscribe { print($0) } .disposed(by: disposeBag)
scan
print("*****reduce*****") Observable.of(10, 100, 1000) .reduce(1, accumulator: +) // 1 + 10 + 100 + 1000 = 1111 .subscribe(onNext: { print($0) }) .disposed(by: disposeBag)
print("*****concat*****") let subject1 = BehaviorSubject(value: "KK") let subject2 = BehaviorSubject(value: "1") let subjectsSubject = BehaviorSubject(value: subject1) subjectsSubject.asObservable() .concat() .subscribe { print($0) } .disposed(by: disposeBag) subject1.onNext("MARK") subject1.onNext("JiaXiaoChao") subjectsSubject.onNext(subject2) subject2.onNext("打印不出来") subject2.onNext("2") subject1.onCompleted() // 必须要等subject1 完成了才能订阅到! 用来控制顺序 网络数据的异步 subject2.onNext("3")
print("*****catchErrorJustReturn*****") let sequenceThatFails = PublishSubject<String>() sequenceThatFails .catchErrorJustReturn("PXWX") .subscribe { print($0) } .disposed(by: disposeBag) sequenceThatFails.onNext("Tony") sequenceThatFails.onNext("KK") // 正常序列发送成功的 //发送失败的序列,一旦订阅到位 返回我们之前设定的错误的预案 sequenceThatFails.onError(self.pxError)
print("*****catchError*****") let recoverySequence = PublishSubject<String>() sequenceThatFails .catchError { print("Error:", $0) return recoverySequence // 获取到了错误序列-我们在中间的闭包操作处理完毕,返回给用户需要的序列(showAlert) } .subscribe { print($0) } .disposed(by: disposeBag) sequenceThatFails.onNext("PXWX") sequenceThatFails.onNext("KK") // 正常序列发送成功的 sequenceThatFails.onError(pxError) // 发送失败的序列 recoverySequence.onNext("Tony")
print("*****retry*****") var count = 1 // 外界变量控制流程 let sequenceRetryErrors = Observable<String>.create { observer in observer.onNext("PXWX") observer.onNext("JXC") observer.onNext("KK") if count == 1 { // 流程进来之后就会过度-这里的条件可以作为出口,失败的次数 observer.onError(self.lgError) // 接收到了错误序列,重试序列发生 print("错误序列来了") count += 1 } observer.onNext("Tony") observer.onNext("Jeak") observer.onNext("WUXU") observer.onCompleted() return Disposables.create() } sequenceRetryErrors .retry() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag)
print("*****debug*****") var count = 1 let sequenceThatErrors = Observable<String>.create { observer in observer.onNext("PXWX") observer.onNext("JXC") observer.onNext("KK") if count < 5 { observer.onError(self.lgError) print("错误序列来了") count += 1 } observer.onNext("Tony") observer.onNext("Jeak") observer.onNext("WUXU") observer.onCompleted() return Disposables.create() } sequenceThatErrors .retry(3) .debug() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) // MARK: 方便调试
print("*****RxSwift.Resources.total*****") print(RxSwift.Resources.total)
高阶函数的用法还是有点意思的!但RxSwift高阶函数(操作符)非常多