RxSwift 系列(五) -- Filtering and Conditional Operators

前言

本篇文章将要学习RxSwift中过滤和条件操作符,在RxSwift中包括了:

  • filter
  • distinctUntilChanged
  • elementAt
  • single
  • take
  • takeLast
  • takeWhile
  • takeUntil
  • skip
  • skipWhile
  • skipWhileWithIndex
  • skipUntil

filter

Observable序列中只发出满足过滤条件的事件。

let disposeBag = DisposeBag()
    
Observable.of(1,2,3,4,5).filter({$0 > 2}).subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

3
4
5

distinctUntilChanged

过滤连续发出的相同事件

let disposeBag = DisposeBag()
Observable.of(1,2,3,3,3,2,2,5,4).distinctUntilChanged().subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

1
2
3
2
5
4

elementAt

Observable只发出指定位置的事件

let disposeBag = DisposeBag()
    
Observable.of(1,2,3,5,4).elementAt(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

3

single

用法一:检测Observable序列是否发射一个元素。如果发射多个,会报错。

let disposeBag = DisposeBag()

// error:Sequence contains more than one element
// Observable.of(1,2,3).single().subscribe(onNext: {print($0)}).disposed(by: disposeBag)

Observable.of(1).single().subscribe(onNext: {print($0)}).disposed(by: disposeBag)

用法二:检测Observable序列发射的元素里面是否包含多个指定的元素,如果有多个或者没有,会报错。

let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
    .single { $0 == "🐸" }
    .subscribe { print($0) }
    .disposed(by: disposeBag)
    
Observable.of("🐱", "🐰", "🐶", "🐱", "🐰", "🐶")
    .single { $0 == "🐒" }
    .subscribe { print($0) }
    .disposed(by: disposeBag)
    
Observable.of("🐱", "🐰", "🐶", "🐶","🐸")
    .single { $0 == "🐶" }
    .subscribe { print($0) }
    .disposed(by: disposeBag)

打印结果:

next(🐸)
completed
error(Sequence doesn't contain any elements.)
next(🐶)
error(Sequence contains more than one element.)

take

Observable序列开始点起只处理前几个事件。

let disposeBag = DisposeBag()
    
Observable.of(1,2,3).take(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

1
2

takeLast

take相反,从Observable序列的结束位置起,只处理前几个事件。

let disposeBag = DisposeBag()    
Observable.of(1,2,3).takeLast(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

2
3

takeWhile

只处理满足条件的事件。感觉和filter没什么区别

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5).takeWhile{$0 < 3}.subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

1
2

takeUntil

直到另一个Observable序列发出一个信号,则原序列终止。

let disposeBag = DisposeBag()
    
let orinSubject = PublishSubject<Int>()
let refreSubject = PublishSubject<Int>()
    
orinSubject.takeUntil(refreSubject).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
    
orinSubject.onNext(1)
orinSubject.onNext(2)
refreSubject.onNext(10)
orinSubject.onNext(3)

打印结果:

1
2

skip

Observable序列的元素跳过指定个数。

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5,6).skip(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

3
4
5
6

skipWhile

跳过满足条件的事件,只要遇见不满足条件的事件,则该事件以及之后的事件(不管是否满足条件)都会发出。

let disposeBag = DisposeBag()    
Observable.of(1,2,3,4,5,6).skipWhile{$0 < 3}.subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

3
4
5
6

skipWhileWithIndex

跳过索引满足条件的事件。

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5,6).skipWhileWithIndex({ (element, index) -> Bool in
        index < 3
    }).subscribe(onNext: {print($0)}).disposed(by: disposeBag)

打印结果:

4
5
6

skipUntil

跳过另一个Observable序列发出事件之前的所有事件。与takeUntil相反。

let disposeBag = DisposeBag()
    
let oriSubject = PublishSubject<Int>()
let refreSubject = PublishSubject<Int>()
    
oriSubject.skipUntil(refreSubject).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
    
oriSubject.onNext(1)
oriSubject.onNext(2)
    
refreSubject.onNext(10)
oriSubject.onNext(3)
oriSubject.onNext(4)

打印结果:

3
4

致谢

如果发现有错误的地方,欢迎评论,感谢!

posted @ 2017-07-21 17:56  Scott_Mr  阅读(526)  评论(0编辑  收藏  举报