Objective
This article will learn RxSwift
about the filter and conditional operators, which are included in the RxSwift
:
filter
distinctUntilChanged
elementAt
single
take
takeLast
takeWhile
takeUntil
skip
skipWhile
skipWhileWithIndex
skipUntil
Filter
Observable
only events that meet the filter conditions are emitted in the sequence.
let disposeBag = DisposeBag() Observable.of(1,2,3,4,5).filter({$0 > 2}).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
345
Distinctuntilchanged
Filter the same events that are emitted continuously
let disposeBag = DisposeBag()Observable.of(1,2,3,3,3,2,2,5,4).distinctUntilChanged().subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
123254
ElementAt
Observable
Issue only events at the specified location
let disposeBag = DisposeBag() Observable.of(1,2,3,5,4).elementAt(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
3
Single
Usage One: detects Observable
whether a sequence emits an element. If you launch multiple, you will get an error.
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)
Usage Two: detects Observable
whether the sequence emitted by the element contains multiple specified elements, if there are multiple or not, will be an error.
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)
Printing results:
next(??)completederror(Sequence doesn‘t contain any elements.)next(??)error(Sequence contains more than one element.)
Take
Observable
only the first few events are processed from the start point of the sequence.
let disposeBag = DisposeBag() Observable.of(1,2,3).take(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
12
Takelast
In take
contrast, from Observable
the end of the sequence, only the first few events are processed.
let disposeBag = DisposeBag() Observable.of(1,2,3).takeLast(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
23
TakeWhile
Only events that meet the criteria are processed. It's no different from the feeling. filter
let disposeBag = DisposeBag()Observable.of(1,2,3,4,5).takeWhile{$0 < 3}.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
12
Takeuntil
Until another Observable
sequence emits a signal, the original sequence terminates.
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)
Printing results:
12
Skip
Observable
skips a specified number of elements from a sequence.
let disposeBag = DisposeBag()Observable.of(1,2,3,4,5,6).skip(2).subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
3456
SkipWhile
Skips an event that satisfies a condition, and whenever an event is encountered that does not meet the condition, the event and subsequent events (whether or not it satisfies the condition) are emitted.
let disposeBag = DisposeBag() Observable.of(1,2,3,4,5,6).skipWhile{$0 < 3}.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
Printing results:
3456
Skipwhilewithindex
Skips the event that the index satisfies the condition.
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)
Printing results:
456
Skipuntil
Skips Observable
all events before another sequence emits an event. With the takeUntil
opposite.
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)
Printing results:
34
Thanks
If you find the wrong place, welcome to comment, thanks!
RxSwift Series (v)