amb
- 当传入多个Observable到amb操作符时, 他将取第一个发生元素或产生事件的Observable, 然后只发出他的元素. 并且忽略其他的Observable
let subject191 = PublishSubject<Int>()
let subject192 = PublishSubject<Int>()
let subject193 = PublishSubject<Int>()
subject191
.amb(subject192)
.amb(subject193)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
subject192.onNext(0)
subject191.onNext(50)
subject192.onNext(1)
subject193.onNext(101)
subject193.onNext(102)
subject191.onNext(51)
subject192.onNext(2)
subject193.onNext(103)
takeWhile
- 该方法依次判断Observable序列的每一个值是否满足给定的条件. 当地一个不满足条件的值出现时, 他便自动完成
Observable.of(1,2,3,4,6,1,3,8,3,5,9)
.takeWhile {
$0 < 7
}
.subscribe(onNext: { print($0) })
.disposed(by: bag)
takeUntil
- 除了订阅Observable外, 通过takeUntil方法我们还可以监视另外一个Observable, 即notifier
- 如果notifier发出值或complete通知, 那么源Observable便自动完成, 停止发送事件
let source21 = PublishSubject<String>()
let notifier21 = PublishSubject<String>()
source21.takeUntil(notifier21)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
source21.onNext("a")
source21.onNext("b")
source21.onNext("c")
notifier21.onNext("A")
source21.onNext("d")
source21.onNext("e")
source21.onNext("f")
skipWhile
- 该方法用于跳过前面所有满足条件的事件
- 一旦遇到不满足的条件的事件, 之后就不会再跳过了
Observable.of(1,2,3,4,5,6,7)
.skipWhile{ $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: bag)
skipUntil
- 同上面的takeUntil一样, skipUntil除了订阅源Observable外, 通过skipUntil方法我们还可以监视另外一个Observable, 即notifier
- 与takeUntil相反的是. 源Observable序列事件默认会一直跳过, 知道notifier发出值或complete通知
let source32 = PublishSubject<Int>()
let notifier32 = PublishSubject<Int>()
source32.skipUntil(notifier32)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
source32.onNext(1)
source32.onNext(2)
source32.onNext(3)
notifier32.onNext(11)
source32.onNext(4)
source32.onNext(5)
notifier32.onNext(12)
source32.onNext(6)
source32.onNext(7)