RxSwift Series (v)

Source: Internet
Author: User

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

Observableonly 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

ObservableIssue 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

Observableonly 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

Observableskips 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.