takeUntil

忽略掉在第二个 Observable 产生事件后发出的那部分元素

takeUntil - 图1

takeUntil 操作符将镜像源 Observable,它同时观测第二个 Observable。一旦第二个 Observable 发出一个元素或者产生一个终止事件,那个镜像的 Observable 将立即终止。


演示

  1. let disposeBag = DisposeBag()
  2. let sourceSequence = PublishSubject<String>()
  3. let referenceSequence = PublishSubject<String>()
  4. sourceSequence
  5. .takeUntil(referenceSequence)
  6. .subscribe { print($0) }
  7. .disposed(by: disposeBag)
  8. sourceSequence.onNext("?")
  9. sourceSequence.onNext("?")
  10. sourceSequence.onNext("?")
  11. referenceSequence.onNext("?")
  12. sourceSequence.onNext("?")
  13. sourceSequence.onNext("?")
  14. sourceSequence.onNext("?")

输出结果:

  1. next(?)
  2. next(?)
  3. next(?)
  4. completed