注意:使用本例中的代码首先应该导入头文件,代码如下:
import RxSwift
Debugging Operators
该类操作帮助调试 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资源分配的计算量,它在开发过程中对于检测泄漏是非常有用的。
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在默认情况下不启用,通常不会启用发布构建。下面是展示如何启用它:
Follow these instructions to enable RxSwift.resourceCount
in your project:
CocoaPods
- 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
- Run
pod update
. - Build project (Product → Build).
Carthage
- Run
carthage build --configuration Debug
. - Build project (Product → Build).