RxSwift五:CombiningOperators

RxSwift练习:https://github.com/flywo/RxSwiftPractice

startWith

let disposeBag = DisposeBag()
        let numbers = Observable.of(2,3,4)
        let observable = numbers.startWith(1)
        observable
            .subscribe(onNext: {print($0)})
            .disposed(by: disposeBag)
        /*
         1
         2
         3
         4
         */

concat

//concat
        let first = Observable.of(1,2,3)
        let second = Observable.of(4,5,6)
        let obserable1 = Observable.concat([first, second])
        obserable1
            .subscribe(onNext: {print($0)})
            .disposed(by: disposeBag)
        /*
         1
         2
         3
         4
         5
         6
         */
        let ger = Observable.of(1,2)
        let ber = Observable.of(3,4)
        let obserable2 = ger.concat(ber)
        obserable2
            .subscribe(onNext: {print($0)})
            .disposed(by: disposeBag)
        /*
         1
         2
         3
         4
         */

merge

//merge
        let left = PublishSubject()
        let right = PublishSubject()
        let soure = Observable.of(left.asObserver(), right.asObserver())
        let observable3 = soure.merge()
        let disposeable = observable3.subscribe(onNext: {print($0)})
        var value1 = ["a","b","c"]
        var value2 = ["d","e","f"]
        repeat {
            if arc4random_uniform(2) == 0 {
                if !value1.isEmpty {
                    left.onNext("left:"+value1.removeFirst())
                }else if !value2.isEmpty {
                    right.onNext("right:"+value2.removeFirst())
                }
            }
        }while !value1.isEmpty || !value2.isEmpty
        disposeable.dispose()
        /*
         left:a
         left:b
         left:c
         right:d
         right:e
         right:f
         */

combineLatest

 //combineLatest
        let l1 = PublishSubject()
        let l2 = PublishSubject()
        let observable4 = Observable.combineLatest(l1, l2) { lastL, lastR in
            "\(lastL) \(lastR)"
        }
        observable4.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        l1.onNext("1")
        l2.onNext("2")
        l1.onNext("3")
        l2.onNext("4")
        /*
         1 2
         3 2
         3 4
         */

let choice: Observable = Observable.of(.short, .long)
        let dates = Observable.of(Date())
        let ob1 = Observable.combineLatest(choice, dates) { format, when -> String in
            let formatter = DateFormatter()
            formatter.dateStyle = format
            return formatter.string(from: when)
        }
        ob1.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        /*
         4/26/18
         April 26, 2018
        */

zip

 //zip
        enum Weather {
            case cloudy
            case sunny
        }
        let l3: Observable = Observable.of(.sunny, .cloudy, .cloudy, .sunny)
        let r2 = Observable.of("Lish", "copen", "lond", "mard", "vien")
        let ob2 = Observable.zip(l3, r2) {"It is \($0) in \($1)"}
        ob2.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        /*
         It is sunny in Lish
         It is cloudy in copen
         It is cloudy in lond
         It is sunny in mard
         */

withLatestFrom

let button = PublishSubject()
        let textField = PublishSubject()
        let ob3 = button.withLatestFrom(textField)
        ob3.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        textField.onNext("Par")
        textField.onNext("par2")
        textField.onNext("par3")
        button.onNext(())
        button.onNext(())
        /*
         par3
         par3
         */

sample

 let button = PublishSubject()
        let textField = PublishSubject()
        let ob3 = textField.sample(button)
        ob3.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        textField.onNext("Par")
        textField.onNext("par2")
        textField.onNext("par3")
        button.onNext(())
        button.onNext(())
        textField.onNext("par4")
        textField.onNext("par5")
        button.onNext(())
        /*只有改变值时,触发才会有效果
         par3
         par5
         */

amb

let amb1 = PublishSubject()
        let amb2 = PublishSubject()
        let obamb = amb1.amb(amb2)
        obamb.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        amb1.onNext("a")
        amb2.onNext("1")
        amb1.onNext("b")
        amb1.onNext("c")
        amb2.onNext("2")
        /*先发出事件队列会阻止后发出事件队列
         a
         b
         c
         */

switchLatest

let one = PublishSubject()
        let two = PublishSubject()
        let three = PublishSubject()
        let soucresl = PublishSubject>()
        let obsl = soucresl.switchLatest()
        obsl.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        soucresl.onNext(one)
        one.onNext("Some text from sequence one")
        two.onNext("Some text from sequence two")
        soucresl.onNext(two)
        two.onNext("More text from sequence two")
        one.onNext("and also from sequence one")
        soucresl.onNext(three)
        two.onNext("Why don't you see me?")
        one.onNext("I'm alone, help me")
        three.onNext("Hey it's three. I win.")
        soucresl.onNext(one)
        one.onNext("Nope. It's me, one!")
        /*soucresl控制发出事件的队列
         Some text from sequence one
         More text from sequence two
         Hey it's three. I win.
         Nope. It's me, one!
         */

reduce

//reduce
        let sourcereduce = Observable.of(1,3,5,7,9)
        sourcereduce.reduce(0, accumulator: +).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        //25

scan

//scan
        let sourcescan = Observable.of(1,3,5,7,9)
        sourcescan.scan(0, accumulator: +).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
        /*
         1
         4
         9
         16
         25
         */

你可能感兴趣的:(RxSwift五:CombiningOperators)