美文网首页RACRXswiftiOS-swift
Swift进阶之RxSwift(四)

Swift进阶之RxSwift(四)

作者: Cocoaleeo | 来源:发表于2017-05-12 16:21 被阅读192次

    前言

    步入正题

    1.Error

    当我们创建Observable的时候,其内部默认是带有三个事件的:

    • onNext
    • onError
    • onCompleted

    正常情况下,在被dispose之前都会执行onNext事件,这也是我们希望的情况,but shit happens! 这是就会执行onError以及complete,从下面的🌰中来看一下具体执行过程!

    • catchErrorJustReturn
      接收到错误是返回特定值,可用于error在预料之中,或者确定不会发生错误的情况。
    let disposeBag = DisposeBag()
    let sequenceThatFails = PublishSubject<String>()
    /// 捕捉到错误后返回😊,执行onCompleted,销毁sequenceThatFails
    sequenceThatFails
        .catchErrorJustReturn("😊")
        .subscribe { print($0) }
        .disposed(by: disposeBag)
    sequenceThatFails.onNext("😬")
    sequenceThatFails.onNext("😨")
    /// 模拟发生错误
    sequenceThatFails.onError(TestError.test)
    /// 打印结果
    next(😬)
    next(😨)
    next(😊)
    completed
    
    • catchError
      当捕捉到错误时,讲执行操作交给另外的Observable
    let disposeBag = DisposeBag()
    /// 创建两个subject
    let sequenceThatFails = PublishSubject<String>()
    let recoverySequence = PublishSubject<String>()
    /// shit happend! 捕捉到error时,返回recoverySequence,sequenceThatFails发送complete,销毁sequenceThatFails。
    sequenceThatFails
        .catchError {
            print("Error:", $0)
            return recoverySequence
        }
        .subscribe { print($0) }
        .disposed(by: disposeBag)
    sequenceThatFails.onNext("😬")
    sequenceThatFails.onNext("😨")
    sequenceThatFails.onError(TestError.test)
    recoverySequence.onNext("😊")
    /// 打印结果
    next(😬)
    next(😨)
    Error: test
    next(😊)
    
    • retry and retry(_:)
      发生错误时进行重试
    let disposeBag = DisposeBag()
    var count = 1
    let sequenceThatErrors = Observable<String>.create { observer in
        observer.onNext("🍎")
        observer.onNext("🍐")
    /// 模拟发生错误  
        if count == 1 {
            observer.onError(TestError.test)
            print("Error encountered")
            count += 1
        }
        observer.onNext("🐶")
        observer.onNext("🐱")
        observer.onCompleted()
        return Disposables.create()
    }
    sequenceThatErrors
        .retry()
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
    

    发生错误是重试,参数为次数

    let disposeBag = DisposeBag()
    var count = 1
    let sequenceThatErrors = Observable<String>.create { observer in
        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)
    

    ** 开发过程中,没有人希望出现错误,但是错误是不可避免的,当发生错误是我们可以通过上述方法来进行补救,我们也可以通过Debug来减少错误的发生!**

    2.Debugging
    • debug
      打印所有的订阅,事件以及销毁操作
    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)
        .debug()
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
    
    • RxSwift.Resources.total
      有点类似于引用计数的东西,默认是不开启的,如果需要使用的话,在cocoapod文件中添加点东西,所以你的Podfile文件看起来应该是这样的
    target 'AppTarget' do
    pod 'RxSwift'
    end
    /// 开启 RxSwift.Resources.total
    post_install do |installer|
        installer.pods_project.targets.each do |target|
            if target.name == 'RxSwift'
                target.build_configurations.each do |config|
                    if config.name == 'Debug'
                        config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['-D', 'TRACE_RESOURCES']
                    end
                end
            end
        end
    end
    

    总结

    终于学完了,本系列文章基本上涵盖了RxSwift官方demo中的所有用法,我这里最多只能起到个抛砖引玉的作用,Rx博大精深,想要用好还是需要下点功夫的!各位加油!

    ** 另外,在ReactiveObjC中有许多方法是与cocoa部分关联的,但是在RxSwift中并不包含,需要引入RxCocoa方可使用!**


    码字好辛苦的,各位不吝赐个赞呗!

    相关文章

      网友评论

        本文标题: Swift进阶之RxSwift(四)

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