Operation之变换操作符

buffer

  • buffer方法的作用是缓冲组合, 第一个参数是缓冲时间, 第二个参数是缓冲个数, 第三个参数是线程
  • 该方法简单来说就是缓存Observable中发出的新元素, 当元素达到某个数量, 或者经过了特定的时间, 他就会将这个元素集合发送出来
let subject = PublishSubject<String>()
subject.buffer(timeSpan: 1, count: 3, scheduler: MainScheduler.instance)
    .subscribe(onNext: {print($0)})
    .disposed(by: bag)

subject.onNext("a")
subject.onNext("b")
subject.onNext("c")

subject.onNext("1")
subject.onNext("2")
subject.onNext("3")

subject.onNext("^_^")
subject.onCompleted()

window

  • window操作符和buffer十分相似. 不过buffer是周期性的将缓存的元素集合发送出来, 而window周期性的将元素集合以Observable的形式发送出来
  • 同时buffer要顶到元素搜集完毕后, 才会发出元素序列. 而window可以实时发出元素序列
let subject2 = PublishSubject<String>()
subject2
    .window(timeSpan: 1, count: 3, scheduler: MainScheduler.instance)
    .subscribe(onNext:{[weak self] in
        print($0)
        $0.subscribe(onNext: {print($0)}).disposed(by: self!.bag)
    })
    .disposed(by: bag)
    subject2.onNext("a")
    subject2.onNext("b")
    subject2.onNext("c")

    subject2.onNext("1")
    subject2.onNext("2")
    subject2.onNext("3")

    subject2.onNext("^_^")
    subject2.onCompleted()

map

  • 该操作符通过传入一个函数闭包把原来的Observable序列转变为一个新的Observable序列
Observable.of(1, 2, 3)
    .map{$0 * 10}
    .subscribe(onNext: { print($0) })
    .disposed(by: bag)

flatMap

  • flatMap操作符会对源Observable的每一个元素应用一个转换方法, 将他们转换成Observables. 然后将这些Observables的元素合并之后在发送出来. 即又将其"拍扁"(降维)成一个Observable序列
let subject41 = BehaviorSubject(value: "A")
let subject42 = BehaviorSubject(value: "1")

let variable4 = Variable(subject41)

variable4.asObservable().flatMap { $0 }.subscribe(onNext: { print($0) }).disposed(by: bag)
subject41.onNext("B")
variable4.value = subject42
subject42.onNext("2")
subject41.onNext("C")

flatMapLatest

  • flatMapLatest 与 flatMap 的唯一区别是: flatMapLatest 只会接收最新的value事件
  • 上面的案例中 flatMap 修改为 flatMapLatest
let subject51 = BehaviorSubject(value: "A")
let subject52 = BehaviorSubject(value: "1")

let variable5 = Variable(subject51)

variable5.asObservable().flatMapLatest{ $0 }.subscribe(onNext: { print($0) }).disposed(by: bag)

subject51.onNext("B")
variable5.value = subject52
subject52.onNext("2")
subject51.onNext("C")

concatMap

  • concatMap 与 flatMap 的唯一区别是: 当前一个Observable元素发送完毕后, 后一个Observable才可以开始发送元素. 或者说等待前一个Observable产生完成事件后, 才对后一个Observable进行订阅
let subject61 = BehaviorSubject(value: "A")
let subject62 = BehaviorSubject(value: "1")

let variable6 = Variable(subject61)

variable6.asObservable().flatMapLatest{ $0 }.subscribe(onNext: { print($0) }).disposed(by: bag)

subject61.onNext("B")
variable6.value = subject62
subject62.onNext("2") // 因为subject61还没有结束, 所以不会打印
subject61.onNext("C")
subject61.onCompleted() // 让subject61完成或者error, 上面的subject62发出的事件才会被监听到

scan

  • scan就是先给一个初始化的数, 然后不断的拿前一个结果和最新的值进行处理操作
Observable.of(1,2,3,4,5)
    .scan(0) { (acum, item) -> Int in
        acum + item
}
.subscribe(onNext: { print($0) })
.disposed(by: bag)

groupBy

  • groupBy 操作符将源Observable分解为多个子Observable, 然后将这些子Observable发送出来
  • 也就是说改操作符会将元素通过某个键进行分组, 然后将分组后的元素序列以Observable的形态发送出来
Observable.of(1,2,3,4,5,7,8,13)
    .groupBy { (item) -> String in
        item % 2 == 0 ? "偶数" : "奇数"
}.subscribe(onNext: { [weak self] group in
    group.asObservable().subscribe(onNext: {event in
        print("group: \(group)")
        print("key: \(group.key)")
        print("event: \(event)")
    }).disposed(by: self!.bag)
}).disposed(by: bag)

posted @ 2019-11-08 15:56  Chaos_G  阅读(216)  评论(0编辑  收藏  举报