阅读 167

RxSwift 小记 Error Handling Operators(catchError,retry)

(*useful)标记:目前觉得有用的函数 //FIXME 标记:待补充 ##帮助捕获错误,并可以发送消息,平滑的处理error


####catchErrorJustReturn

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(😊) //捕获的error completed

####catchError: 捕获错误,并return一个Observable sequence

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)
    sequenceThatFails.onNext("errored")  
    recoverySequence.onNext("😊")
}
复制代码

打印: next(😬) next(😨) next(😡) next(🔴) Error: test //切换到了recoverySequence next(😊)

####retry
#####重复订阅onError的sequence,indefinitely(可无限订阅

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 🍎 //retry从这里开始 ,demo只进行了一个次error 🍐 🍊 🐶 🐱 🐭

####retry(_:) 跟上面的一样,加入了参数(控制retry的次数)

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 Unhandled error happened: test subscription called from:

文章分类
阅读
文章标签