十一、調(diào)試操作-Debugging Operators(Rx.playground翻譯)

注意:使用本例中的代碼首先應(yīng)該導(dǎo)入頭文件,代碼如下:

import RxSwift

Debugging Operators

該類操作幫助調(diào)試 Rx 代碼。

debug

打印出所有訂閱,事件和處理。

example("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(Error.Test)
            print("Error encountered")
            count += 1
        }

        observer.onNext("")
        observer.onNext("")
        observer.onNext("")
        observer.onCompleted()

        return NopDisposable.instance
    }

    sequenceThatErrors
        .retry(3)
        .debug()
        .subscribeNext { print($0) }
        .addDisposableTo(disposeBag)
}

Debug Area 輸出:
--- debug example ---
2016-06-28 17:40:52.902: playground19.swift:42 (__lldb_expr_19) -> subscribed
2016-06-28 17:40:52.904: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
2016-06-28 17:40:52.904: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
2016-06-28 17:40:52.905: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
Error encountered
2016-06-28 17:40:52.908: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
2016-06-28 17:40:52.908: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
2016-06-28 17:40:52.909: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
Error encountered
2016-06-28 17:40:52.911: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
2016-06-28 17:40:52.912: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
2016-06-28 17:40:52.912: playground19.swift:42 (__lldb_expr_19) -> Event Next(??)
??
Error encountered
2016-06-28 17:40:52.915: playground19.swift:42 (__lldb_expr_19) -> Event Error(Test)
2016-06-28 17:40:52.915: playground19.swift:42 (__lldb_expr_19) -> disposed


RxSwift.resourceCount

提供了所有Rx資源分配的計(jì)算量,它在開發(fā)過程中對于檢測泄漏是非常有用的。

example("RxSwift.resourceCount") {
    print(RxSwift.resourceCount)
    
    let disposeBag = DisposeBag()
    
    print(RxSwift.resourceCount)
    
    let variable = Variable("??")
    
    let subscription1 = variable.asObservable().subscribeNext { print($0) }
    
    print(RxSwift.resourceCount)
    
    let subscription2 = variable.asObservable().subscribeNext { print($0) }
    
    print(RxSwift.resourceCount)
    
    subscription1.dispose()
    
    print(RxSwift.resourceCount)
    
    subscription2.dispose()
    
    print(RxSwift.resourceCount)
}
    
print(RxSwift.resourceCount)

Debug Area 輸出:
--- RxSwift.resourceCount example ---
0
1
??
3
??
4
3
2
0

RxSwift.resourceCount在默認(rèn)情況下不啟用,通常不會啟用發(fā)布構(gòu)建。下面是展示如何啟用它:

Follow these instructions to enable RxSwift.resourceCount in your project:

CocoaPods

  1. Add a post_install hook to your Podfile, e.g.:
target 'AppTarget' do
pod 'RxSwift'
end

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
  1. Run pod update.
  2. Build project (ProductBuild).

Carthage

  1. Run carthage build --configuration Debug.
  2. Build project (ProductBuild).
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容