RxSwift Observable的创建

Observable的创建

常用的为:deferred、of、just

  • empty
        //------   empty   ------
        //MARK: empty
        //空序列,只能completed
        let emptyOb = Observable.empty()
        _ = emptyOb.subscribe(onNext: { (number) in
            print("订阅:", number)
        }, onError: { (error) in
            print("error:", error)
        }, onCompleted: {
            print("completed")
        }, onDisposed: {
            print("disposed")
        })
  • just
        //------   just   ------
        //MARK: just
        //单个信号序列
        let array = ["a", "b", "c", "d", "e"]
        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("completed")
            }, onDisposed: {
                print("disposed")
            })
  • of
        //------   of   ------
        //MARK: of
        //多个元素
        Observable.of("a", "b")
            .subscribe { (event) in
                print(event)
        }.disposed(by: disposeBag)
        
        //字典
        Observable<[String: Any]>.of(["name": "aa", "age": 18])
            .subscribe { (event) in
                print(event)
        }.disposed(by: disposeBag)
        
        //数组
        Observable<[String]>.of(["a", "b", "c"])
            .subscribe { (event) in
                print(event)
            }.disposed(by: disposeBag)
  • from
        //------   from   ------
        //MARK: from
        //从集合中获取序列,有可选项
        Observable<[String]>.from(optional: ["a", "b"])
            .subscribe { (event) in
                print(event)
        }.disposed(by: disposeBag)
        
        //------   deferred   ------
        //MARK: deferred
        //根据条件判断业务逻辑,根据业务选择序列
        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)
            })
  • range
        //------   range   ------
        //MARK: range
        //生成指定范围内的可观察整数序列
        Observable.range(start: 2, count: 5)
            .subscribe { (event) in
                print(event)
        }.disposed(by: disposeBag)
  • generate
        //------   generate   ------
        //MARK: generate
        //当所有条件都为true时才给出Observable序列,类似数组遍历循环
        Observable.generate(initialState: 0,  //初始值
                            condition: { $0 < 10 },  //条件1
                            iterate: { $0 + 2 })  //条件2
            .subscribe { (event) in
                print(event)
        }.disposed(by: disposeBag)
        
        //数组遍历
        let arr = ["aa", "bb", "cc", "dd", "ee", "ff"]
        Observable.generate(initialState: 0,
                            condition: { $0 < arr.count },
                            iterate: { $0 + 1 })
            .subscribe { (event) in
                print(event)
        }.disposed(by: disposeBag)
  • timer
        //------   timer   ------
        //MARK: timer
        //para1:第一次响应距离现在的时间;para2:时间间隔;para3:线程
        Observable.timer(5, period: 2, scheduler: MainScheduler.instance)
            .subscribe { (event) in
                print(event)
            }
            .disposed(by: disposeBag)
        
        //没有指定period,为一次性执行
        Observable.timer(2, scheduler: MainScheduler.instance)
            .subscribe { (event) in
                print(event)
            }
            .disposed(by: disposeBag)
  • interval
        //------   interval   ------
        //MARK: interval
        //从当前时间开始
        Observable.interval(2, scheduler: MainScheduler.instance)
            .subscribe { (event) in
                print(event)
            }
            .disposed(by: disposeBag)
  • repeatElement
        //------   repeatElement   ------
        //MARK: repeatElement
        //创建一个可以无限发出给定元素的Event的Observable序列,永不终止
        Observable.repeatElement(5)
            .subscribe { (event) in
                print(event)
            }
            .disposed(by: disposeBag)
  • error
        //------   error   ------
        //MARK: error
        //发出错误信号
        Observable.error(NSError.init(domain: "error", code: 1002, userInfo: ["reason": "unknow"]))
            .subscribe { (event) in
                print(event)
            }
            .disposed(by: disposeBag)
  • never
        //------   never   ------
        //MARK: never
        //创建永远不会发出也不会终止的Observable序列;
        //这种类型的响应源,在测试或者组合操作符中禁用确切的源很有用
        Observable.never()
            .subscribe { (event) in
                print(event)
            }
            .disposed(by: disposeBag)

你可能感兴趣的:(RxSwift Observable的创建)