RxSwift干货RXswift

RxSwift文档翻译8 - Error Handling Op

2017-06-02  本文已影响64人  小白猿

前言

本节中主要介绍错误处理

// 定义一个错误
public enum TestError : Error {
         case test
    }

Error Handling Operators

example("catchErrorJustReturn") {
    let disposeBag = DisposeBag()
    
    let sequenceThatFails = PublishSubject<String>()
    
    sequenceThatFails
        .catchErrorJustReturn("😊")
        .subscribe { print($0) }
        .disposed(by: disposeBag)
    
    sequenceThatFails.onNext("😬")
    sequenceThatFails.onNext("😨")
    sequenceThatFails.onNext("😡")
    sequenceThatFails.onNext("🔴")
    sequenceThatFails.onError(TestError.test)
}

// 控制台打印的结果:
--- catchErrorJustReturn example ---
next(😬)
next(😨)
next(😡)
next(🔴)
next(😊)
completed
 example("catchError") {
            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.onNext("🔴")
            sequenceThatFails.onError(TestError.test)
            
            recoverySequence.onNext("😊")
        }

// 控制台打印的结果:
--- catchError example ---
next(😬)
next(😨)
next(😡)
next(🔴)
Error: test
next(😊)
example("retry") {
    let disposeBag = DisposeBag()
    var count = 1
    
    let sequenceThatErrors = Observable<String>.create { observer in
        observer.onNext("🍎")
        observer.onNext("🍐")
        observer.onNext("🍊")
        
        if count == 1 {
            observer.onError(TestError.test)
            print("Error encountered")
            count += 1
        }
        
        observer.onNext("🐶")
        observer.onNext("🐱")
        observer.onNext("🐭")
        observer.onCompleted()
        
        return Disposables.create()
    }
    
    sequenceThatErrors
        .retry()
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

// 控制台打印的结果:
--- retry example ---
🍎
🍐
🍊
Error encountered
🍎
🍐
🍊
🐶
🐱
🐭

为了展示周而复始的过程,我修改了一下代码

example("retry") {
    let disposeBag = DisposeBag()
    var count = 1

    let sequenceThatErrors = Observable<String>.create { observer in
        observer.onNext("🍎")
        observer.onNext("🍐")
        observer.onNext("🍊")

/***** 在增加一次错误,看其如何处理 ***/
        if count == 2{
            observer.onError(TestError.test)
            print("Error encountered")
            count += 1
        }
/*********************************/
        if count == 1 {
            observer.onError(TestError.test)
            print("Error encountered")
            count += 1
        }

        observer.onNext("🐶")
        observer.onNext("🐱")
        observer.onNext("🐭")
        observer.onCompleted()

        return Disposables.create()
    }

    sequenceThatErrors
        .retry()
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

// 控制台打印的结果:
--- retry example ---
🍎
🍐
🍊
Error encountered
🍎
🍐
🍊
Error encountered*****
🍎
🍐
🍊
🐶
🐱
🐭
example("retry maxAttemptCount") {
            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.onNext("🐭")
                observer.onCompleted()
                
                return Disposables.create()
            }
            
            sequenceThatErrors
                .retry(3)
                .subscribe(onNext: { print($0) })
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- retry maxAttemptCount example ---
🍎
🍐
🍊
Error encountered
🍎
🍐
🍊
Error encountered
🍎
🍐
🍊
Error encountered
``

[下一篇:RxSwift文档翻译9 - Debugging Operators ](http://www.jianshu.com/p/75aefe7dc484)
上一篇下一篇

猜你喜欢

热点阅读