RxSwift ErrorHandding Operators

目录:

RxSwift - 入门
RxSwift Observable Create
RxSwift Subject
RxSwift Combination Operators
RxSwift Transforming Operators
RxSwift Filterning and conditional operators
RxSwift Mathematical and aggregate operators
RxSwift Connectable Operators
RxSwift ErrorHandding Operators
RxSwift Debug Operators


Demo地址


catchErrorJustReturn

当捕获到异常时返回特定的值,这个是就是参数

example("catchErrorJustReturn") {
    let bag = DisposeBag()
    
    let sequenceThatFails = PublishSubject()
    
    sequenceThatFails
        .catchErrorJustReturn(-1)
        .subscribe(
            onNext: { print($0) },
            onError: { print($0) },
            onCompleted: { print("onCompleted") },
            onDisposed: { print("onDisposed") }
        )
        .disposed(by: bag)
    
    sequenceThatFails.onNext(1)
    sequenceThatFails.onNext(2)
    sequenceThatFails.onNext(3)
    sequenceThatFails.onError(TestError.test)
}
---------- catchErrorJustReturn ----------
1
2
3
-1
onCompleted
onDisposed

通过上面打印信息我们可以看到,在发生了error事件之后,就会返回我们设置好的参数,然后结束掉整个Observable

catchErrorJustReturn.png


catchError

顾名思义,捕获Error,不同于catchErrorJustReturncatchError可以才closure中进行处理,可以用于一些更复杂的情况。

 example("catchError") {
            let bag = DisposeBag()
            
            let sequenceThatFails = PublishSubject()
            let recoverySequence = PublishSubject()
            
            sequenceThatFails
                .catchError {
                    print("Error:", $0)
                    return recoverySequence
                }
                .subscribe (
                    onNext: { print($0) },
                    onError: { print($0) },
                    onCompleted: { print("onCompleted") },
                    onDisposed: { print("onDisposed") }
                )
                .disposed(by: bag)
            
            sequenceThatFails.onNext("A")
            sequenceThatFails.onNext("B")
            sequenceThatFails.onNext("C")
            sequenceThatFails.onNext("D")
            sequenceThatFails.onError(TestError.test)
            
            recoverySequence.onNext("1")
        }
---------- catchError ----------
A
B
C
D
Error: test
1
onDisposed
catchError.png


retry

当遇到onError事件之后,会从头开始在进行重试,知道成功为止(不发送onError事件)

       example("retry") {
            let bag = DisposeBag()
            
            let sequenceThatErrors = Observable.create { observer in
                observer.onNext("A")
                observer.onNext("B")
                observer.onNext("C")
                observer.onNext("D")
                // 这里如果打开,就会一直发送A B C D
//                observer.onError(TestError.test)
                
                observer.onNext("1")
                observer.onNext("2")
                observer.onNext("3")
                observer.onCompleted()
                
                return Disposables.create()
            }
            
            sequenceThatErrors
                .retry()
                .subscribe(
                    onNext: { print($0) },
                    onError: { print($0) },
                    onCompleted: { print("onCompleted") },
                    onDisposed: { print("onDisposed") }
                )
                .disposed(by: bag)
        }
//不打开onError
---------- retry ----------
A
B
C
D
1
2
3
onCompleted
onDisposed

//打开onError
---------- retry ----------
A
B
C
D
A
B
C
D
.....

retry.png


retry(_:)

不同于上面的retryretry(_:)要用户传入一个参数,表示重试的次数,需要注意1表示不重试,2表示重试一次,所以次数等于 = n - 1

example("retry maxAttemptCount") {
            let bag = DisposeBag()
            
            let sequenceThatErrors = Observable.create { observer in
                observer.onNext("A")
                observer.onNext("B")
                observer.onNext("C")
                observer.onNext("D")
                
                observer.onError(TestError.test)
                
                observer.onNext("1")
                observer.onNext("2")
                observer.onNext("3")
                observer.onCompleted()
                
                return Disposables.create()
            }
            
            sequenceThatErrors
                .retry(2)
                .subscribe(
                    onNext: { print($0) },
                    onError: { print($0) },
                    onCompleted: { print("onCompleted") },
                    onDisposed: { print("onDisposed") }
                )
                .disposed(by: bag)
        }
---------- retry maxAttemptCount ----------
A
B
C
D
A
B
C
D
test
onDisposed
retry(_).png

你可能感兴趣的:(RxSwift ErrorHandding Operators)