RxSwift Series (four)

Source: Internet
Author: User
Tags closure

Objective

This article will learn about RxSwift the four conversion operators:

    • map
    • flatMap
    • flatMapLatest
    • scan
Map

Converts the original Observable sequence to a new sequence by using a closure function Observable .

let disposeBag = DisposeBag()        Observable.of(1,2,3).map({return 10 * $0}).subscribe({print($0)}).disposed(by: disposeBag)

Printing results:

next(10)next(20)next(30)completed
FlatMap

Converts a Observable sequence to another Observable sequence, and merges two Observable sequences. Elements emitted by two sequences are received in chronological order.

let disposeBag = DisposeBag()    struct Player {    var score:Variable<Int>}    let man = Player(score: Variable(80))let women = Player(score: Variable(90))    let player = Variable(man)    player.asObservable().flatMap({$0.score.asObservable()}).subscribe(onNext: {print($0)}).disposed(by: disposeBag)    man.score.value = 85player.value = womenman.score.value = 95women.score.value = 100

Printing results:

80859095100
Flatmaplatest

flatMapLatestThe same way flatMap , a sequence is converted to another sequence, and flatMapLatest only events are emitted from the most recent sequence.

flatMapLatestmap +switchLatest

    • Question: Feeling and switchLatest no difference ah. ( is there any big guy to help answer it? )
let disposeBag = DisposeBag()    struct Player {    var score:Variable<Int>}    let man = Player(score: Variable(80))let women = Player(score: Variable(90))    let player = Variable(man)    player.asObservable().flatMapLatest({$0.score.asObservable()}).subscribe(onNext: {print($0)}).disposed(by: disposeBag)    man.score.value = 85player.value = womenman.score.value = 95women.score.value = 100

Printing results:

808590100
Scan

scanis to provide an initialization value, and then use a computed closure to continually process the previous and subsequent elements and return the processing results as a sequence of individual elements Observable .

let disposeBag = DisposeBag()    Observable.of(10, 100, 1000)    .scan(2) { aggregateValue, newValue in        aggregateValue + newValue    }    .subscribe(onNext: { print($0) })    .disposed(by: disposeBag)

Printing results:

121121112
Thanks

If you find the wrong place, welcome to comment, thanks!

RxSwift Series (four)

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.