RXSwift-Observable的创建(二)

通过上一篇探索RXSwift核心逻辑(一)的介绍,相信小伙伴们对于,RxSwift这个函数响应式编程框架有了一个较为深刻的认识,今天我们来介绍下RxSwift中各种Observable 可观察序列的创建,希望能够带给各位小伙伴一些帮助。

Observable创建

empty

let emtyOb = Observable.empty()
        _ = emtyOb.subscribe(onNext: { (number) in
            print("订阅:",number)
        }, onError: { (error) in
            print("error:",error)
        }, onCompleted: {
            print("完成回调")
        }) {
            print("释放回调")
        }
//打印 完成回调 释放回调
  • 首先来一个空的序列 - 本来序列事件是Int类型的,这里调用emty函数 没有序列,只能complete

just

let array = ["AAA","BBB"]
        Observable<[String]>.just(array)
            .subscribe { (event) in
                print(event)
            }.disposed(by: disposeBag)
        
        _ = Observable<[String]>.just(array).subscribe(onNext: { (number) in
            print("订阅:",number)
        }, onError: { (error) in
            print("error:",error)
        }, onCompleted: {
            print("完成回调")
        }) {
            print("释放回调")
        }
//打印
//next(["AAA", "BBB"])
//completed
//订阅: ["AAA", "BBB"]
//完成回调
//释放回调
  • just单个信号序列创建,订阅一次数据后立刻自动调用completed,销毁内存
  Observable.of("AAA","BBB")
            .subscribe { (event) in
                print(event)
            }.disposed(by: disposeBag)
        
        let ob = Observable.of("AAA","BBB")
        _ = ob.subscribe(onNext: { (element) in
            print(element)
        },  onCompleted: {
            print("完成回调")
        }, onDisposed: {
            print("释放回调")
        })
********of********
//next(AAA)
//next(BBB)
//completed
//订阅(AAA)
//订阅(BBB)
//完成回调
//释放回调
  • of针对多个元素的序列处理
Observable<[String]>.from(optional: ["AAA","BBB"])
            .subscribe { (event) in
                print(event)
            }.disposed(by: disposeBag)

*from从集合中获取序列: 数组,集合,set 获取序列 - 有可选项处理 - 更安全

        var isOdd = true
        _ = Observable.deferred { () -> Observable in
            // 这里设计我们的序列
            isOdd = !isOdd
            if isOdd {
                return Observable.of(1,3,5,7,9)
            }
            return Observable.of(0,2,4,6,8)
            }
            .subscribe { (event) in
                print(event)
            }
//next(0)
//next(2)
//next(4)
//next(6)
//next(8)
//completed

  • 使用deferred()方法延迟Observable序列的初始化,通过传入的block来实现Observable序列的初始化并且返回。
Observable.range(start: 2, count: 5)
            .subscribe { (event) in
                print(event)
            }.disposed(by: disposeBag)
//打印
//next(2)
//next(3)
//next(4)
//next(5)
//next(6)
//completed
  • range生成指定范围内的可观察整数序列
        Observable.generate(initialState: 0,// 初始值
                            condition: { $0 < 10}, // 条件1
                            iterate: { $0 + 2 })  // 条件2 +2
            .subscribe { (event) in
                print(event)
            }.disposed(by: disposeBag)
//打印
//next(0)
//next(2)
//next(4)
//next(6)
//next(8)
//completed
 let arr = ["AAA_1","AAA_2","AAA_3","AAA_4","AAA_5","AAA_6","AAA_7","AAA_8","AAA_9","AAA_10"]
        Observable.generate(initialState: 0,// 初始值
            condition: { $0 < arr.count}, // 条件1
            iterate: { $0 + 1 })  // 条件2 +2
            .subscribe(onNext: {
                print("遍历arr:",arr[$0])
            })
            .disposed(by: disposeBag)
  • generate该方法创建一个只有当提供的所有的判断条件都为 true 的时候,才会给出动作的 Observable 序列。初始值给定 然后判断条件1 再判断条件2 会一直递归下去,直到条件1或者条件2不满足,类似for循环遍历数组
        // 第一次参数:第一次响应距离现在的时间 也就是延迟几秒处罚
        // 第二个参数:时间间隔
        // 第三个参数:线程
        let timer = Observable.timer(1, period: 1, scheduler: MainScheduler.instance)
            .takeUntil(.exclusive, predicate: { (num) -> Bool in
            if (num == self.countDownSeconds+1) {
                return true
            }else{
                return false
            }
        })
            .map{self.countDownSeconds - $0}
            .filter { $0 >= 0 }
            .asObservable()
        
        let second = btn.rx.tap.flatMapLatest { _ -> Observable in

            return timer
        }
        
        second.map { $0 == 0 ? "发送验证码":"再次发送(\($0)s)"}.bind(to: self.btn.rx.title()).disposed(by: disposeBag)

        second.map{ $0 == 0 ? true : false }.bind(to: self.btn.rx.isEnabled).disposed(by: disposeBag)
        
        timer.subscribe { (event) in
            print(event)
        }.disposed(by: disposeBag)
next(10)
next(9)
next(8)
next(7)
next(6)
next(5)
next(4)
next(3)
next(2)
next(1)
next(0)
completed
  • 上图为利用timer实现的登录按钮发送验证码功能,利用takeUntil ()函数为闭包返回true时发送完成

以上为RxSwift-Observable创建中的一些api的介绍,未完待续。

你可能感兴趣的:(RXSwift-Observable的创建(二))