RxSwift Series (eight)

Source: Internet
Author: User

Objective

In this article we will learn RxSwift about error handling, including:

    • Catcherrorjustreturn
    • Catcherror
    • Retry
    • Retry (_:)
Catcherrorjustreturn

errorWhen an event is encountered, it returns a value and ends. More details

let disposeBag = DisposeBag()    let sequenceFail = PublishSubject<String>()    sequenceFail.catchErrorJustReturn("??").subscribe(onNext: {print($0)}).disposed(by: disposeBag)    sequenceFail.onNext("??")sequenceFail.onNext("??")sequenceFail.onError(TestError.test)

Operation Result:

??????
Catcherror

The capture error is processed and can be returned sequence to another subscription. More details

let disposeBag = DisposeBag()    let sequenceThatFails = PublishSubject<String>()let recoverySequence = PublishSubject<String>()    sequenceThatFails.catchError {        print("Error:", $0)        return recoverySequence    }.subscribe { print($0) }.disposed(by: disposeBag)    sequenceThatFails.onNext("??")sequenceThatFails.onNext("??")sequenceThatFails.onNext("??")sequenceThatFails.onError(TestError.test)    recoverySequence.onNext("??")

Operation Result:

next(??)next(??)next(??)Error: testnext(??)
Retry

Encountering the error event can be retried, such as a failed network request and can be reconnected. More details

let disposeBag = DisposeBag()var count = 1    let retrySequence = Observable<String>.create { (observer) -> Disposable in    observer.onNext("??")    observer.onNext("??")            if count == 1 {        observer.onError(TestError.test)        print("Error encountered")        count += 1    }            observer.onNext("??")    observer.onNext("??")            return Disposables.create()}    retrySequence.retry().subscribe(onNext: {print($0)}).disposed(by: disposeBag)

Operation Result:

????Error encountered????????
Retry (_:)
And retryIs the same, only the number of retries is specified.
let disposeBag = DisposeBag()var count = 1    let sequenceThatErrors = Observable<String>.create { observer in    observer.onNext("??")    observer.onNext("??")    observer.onNext("??")            if count < 5 {        observer.onError(TestError.test)        print("Error encountered")        count += 1    }            observer.onNext("??")    observer.onNext("??")    observer.onCompleted()            return Disposables.create()}    sequenceThatErrors.retry(3).subscribe(onNext: { print($0) }).disposed(by: disposeBag)

Operation Result:

??????Error encountered??????Error encountered??????Error encounteredReceived unhandled error: Playground.xcplaygroundpage:41:__lldb_expr_18 -> test
Thanks

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

RxSwift Series (eight)

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.