swiftrx 操作符

flapmap 操作符将源 Observable 的每一个元素应用一个转换方法,将他们转换成 Observables。 然后将这些 Observables 的元素合并之后再发送出来。

  func flapmap(){
    let result =  text_field.rx.text.orEmpty.flatMap {[weak self](text) ->Observable in
        return (self?.requestMessage(str: text ))!.observeOn(MainScheduler.instance)
    }.share()
        result.subscribe(onNext: { (element) in
            print("订阅到:\(element)")
        }).disposed(by: bag)
        
        result.subscribe(onNext: {(element) in
            print("订阅到:\(element) - \(Thread.current)")
        }).disposed(by: bag)
    }
    func requestMessage(str:String)->Observable{
        print("开始请求")

        return Observable.create { (observe) -> Disposable in
            
            observe.onNext("发送信息")
            return Disposables.create()
        };
}

drive 是一个精心准备的特征序列。它主要是为了简化 UI 层的代码。有如下特征:

  • 不会产生 error 事件
  • 一定在 MainScheduler 监听(主线程监听)
  • 可以共享
  func driveSs(){
        let result = text_field.rx.text.orEmpty.asDriver()
        let result1 = result.flatMap { [weak self](text) -> Driver in
            return (self?.requestMessage(str: text))!.asDriver(onErrorJustReturn: "455")
        }
        result1.map{"come \($0)"}.drive(lab.rx.text).disposed(by: bag)
        
        let _ret: BehaviorRelay = BehaviorRelay(value: "")
        
        var ret: Driver {
            return _ret.asDriver().distinctUntilChanged()
        }
        
        ret.asObservable().subscribe(onNext: { [weak self] dateRange in
            
            print(dateRange)
        }).disposed(by: bag)
        result1.map{"comesss \($0)"}.drive(_ret).disposed(by: bag)

    }

publish 会将 Observable 转换为可被连接的 Observable可被连接的 Observable 和普通的 Observable 十分相似,不过在被订阅后不会发出元素,直到 操作符被应用为止。这样一来你可以控制 Observable 在什么时候开始发出元素。

 func publish(){
        let intSequence = Observable.interval(1, scheduler: MainScheduler.instance)
            .publish()
        
        _ = intSequence
            .subscribe(onNext: { print("Subscription 1:, Event: \($0)") })

        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            _ = intSequence.connect()
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
            _ = intSequence
                .subscribe(onNext: { print("Subscription 2:, Event: \($0)") })

        }
        intSequence.connect().disposed(by: bag)

    }
//打印结果
//Subscription 1:, Event: 0
//Subscription 1:, Event: 1
//Subscription 1:, Event: 2
//Subscription 1:, Event: 3
//Subscription 2:, Event: 3
//Subscription 1:, Event: 4
//Subscription 2:, Event: 4
//Subscription 1:, Event: 5
//Subscription 2:, Event: 5

combineLatest 将多个 Observables 中最新的元素通过一个函数组合起来,然后将这个组合的结果发出来。

//会等first,second都发出信号才会组合结果响应出来
//打印结果 2A 2B
 func combine(){
        let first = PublishSubject()
        let second = PublishSubject()
        Observable.combineLatest(first, second){ $0 + $1}
            .subscribe(onNext: { str in
                print(str)
            }).disposed(by: bag)
        first.onNext("1")
     first.onNext("2")
        second.onNext("A")
        second.onNext("B")  
    }

你可能感兴趣的:(swiftrx 操作符)