RxSwift Series (vi)

Source: Internet
Author: User

Objective

This article will learn RxSwift about the math and set operators, which are included in the RxSwift :

    • toArray
    • reduce
    • concat
ToArray

Converts a Observable sequence into an array and converts it to a new Observable sequence launch, and then ends.

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

Operation Result:

[1, 2, 3, 4, 5]
Reduce

Using an initial value and an operator, Observable all elements in the sequence are accumulated and converted into a single event signal. (PS: And map Some of the difference is: map to operate on a single element, reduce for all elements accumulated operations)

let disposeBag = DisposeBag()    Observable.of(1,10,100).reduce(1, accumulator: +).subscribe(onNext: {print($0)}).addDisposableTo(disposeBag)

Operation Result:

112
Concat

Observablemerges two sequences into a Observable sequence, and the Observable elements in another sequence are emitted when all elements of a sequence are successfully launched Observable .
Before the first Observable launch is complete, the second Observable emitted event is ignored, but it receives Observable Observable the last event of the second launch before the first one is completed.
Not a good idea, for example:

let disposeBag = DisposeBag()    let subject1 = BehaviorSubject(value: "??")let subject2 = BehaviorSubject(value: "??")    let variable = Variable(subject1)    variable.asObservable()    .concat()    .subscribe { print($0) }    .disposed(by: disposeBag)    subject1.onNext("??")subject1.onNext("??")    variable.value = subject2subject2.onNext("I would be ignored")subject2.onNext("??")    subject1.onCompleted()subject2.onNext("??")

Operation Result:

next(??)next(??)next(??)next(??)next(??)

Explanation: subject1 Before the launch completes the event

variable.value = subject2subject2.onNext("I would be ignored")subject2.onNext("??")

subject2The emitted events are ignored, but the subject2 last event of the launch is received, and therefore printed onnext(??) .

Thanks

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

RxSwift Series (vi)

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.