美文网首页程序员iOS Developer征服iOS
RxSwift学习之旅-错误处理

RxSwift学习之旅-错误处理

作者: ripple_k | 来源:发表于2017-06-26 11:13 被阅读571次

开始之前首先开启Playground的needsIndefiniteExecution,以保证我们之后的延时操作能够正常运行,在你的Playground添加以下代码

import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true
retry

先来看看 retry ,可能这个操作会比较常用,一般用在网络请求失败时,再去进行请求。
retry 就是失败了再尝试,重新订阅一次序列。

example(of: "retry") {
    var count = 1
    let funnyLookingSequence = Observable<Int>.create({ (observer) -> Disposable in
        let error = NSError(domain: "test", code: 0, userInfo: nil)
        observer.onNext(1)
        observer.onNext(2)
        observer.onNext(3)
        if count < 2 {
            observer.onError(error)
            count += 1
        }
        observer.onNext(4)
        observer.onNext(5)
        observer.onNext(6)
        return Disposables.create()
    })
    
    funnyLookingSequence.retry(2).subscribe({
        print($0)
    })
}
--- Example of: retry ---
next(1)
next(2)
next(3)
next(1)
next(2)
next(3)
next(4)
next(5)
next(6)

需要注意的是不加参数的 retry 会无限尝试下去。我们还可以传递一个 Int 值,来说明最多尝试几次。像这样 retry(2) ,最多尝试两次。

cacheError

出现 Error 时,用一个新的序列替换。

example(of: "cacheError") {
    let sequenceThatFails = PublishSubject<Int>()
    let recoverySequence = Observable.of(100,200,300)
    
    sequenceThatFails.catchError({ (error) -> Observable<Int> in
        return recoverySequence
    }).subscribe({
        print($0)
    })
    
    sequenceThatFails.onNext(1)
    sequenceThatFails.onNext(2)
    sequenceThatFails.onNext(3)
    sequenceThatFails.onError(NSError(domain: "test", code: 2, userInfo: nil))
    sequenceThatFails.onNext(4)
}
--- Example of: cacheError ---
next(1)
next(2)
next(3)
next(100)
next(200)
next(300)
completed

catchError 中你需要传递一个返回序列的闭包。

catchErrorJustReturn

catchErrorJustReturn遇到错误,返回一个值替换这个错误。

example(of: "catchErrorJustReturn") {
    let sequenceThatFails = PublishSubject<Int>()
    
    sequenceThatFails.catchErrorJustReturn(100).subscribe({
        print($0)
    })
    
    sequenceThatFails.onNext(1)
    sequenceThatFails.onNext(2)
    sequenceThatFails.onNext(3)
    sequenceThatFails.onError(NSError(domain: "test", code: 2, userInfo: nil))
}
--- Example of: catchErrorJustReturn ---
next(1)
next(2)
next(3)
next(100)
completed

相关文章

  • RxSwift学习之旅-错误处理

    开始之前首先开启Playground的needsIndefiniteExecution,以保证我们之后的延时操作能...

  • RxSwift学习之旅-初见RxSwift

    概念性的东西就不在这里做过多的陈述了,在这里只说明两点: RxSwift究竟是什么 RxSwift is a li...

  • RxSwift学习之旅-Operators

    Operators是RxSwift的基本组成部分,它的重要性就不言而喻了,前面只是提到在我们开发中,当收到obse...

  • RxSwift学习之旅-Scheduler

    先给ObservableType添加两个扩展方法,以便更好的观察线程之间的切换 接着创建一个水果类的可观察序列,并...

  • 需要具备知识

    RXSwift 学习

  • Error Handling

    一旦序列里面产出了一个 error 事件,整个序列将被终止。RxSwift 主要有两种错误处理机制: retry ...

  • RxSwift学习(一)

    学习RXSwift比较好的文章:RxSwift 系列(一) -- ObservablesRxSwift 系列(二)...

  • 2021-12-15

    RxSwift 学习资料: https://beeth0ven.github.io/RxSwift-Chinese...

  • RxSwift-Timer源码分析

    要学习RxSwift-Timer的实现逻辑,可以先看RxSwift核心逻辑分析。理解RxSwift核心逻辑后,在来...

  • RxSwift(四)-- RxSwift几个常用高阶函数介绍

    对于RxSwift的重点学习,我们还得需要知道RxSwift的高阶函数,掌握好了RxSwift的高阶函数,是你通往...

网友评论

    本文标题:RxSwift学习之旅-错误处理

    本文链接:https://www.haomeiwen.com/subject/gdidcxtx.html