前言
本文主要介绍一些调试相关的操作
- **debug **
会打印所有的订阅,时间 和 disposals
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(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)
}
// 控制台打印的结果:
--- debug example ---
2017-06-02 15:20:10.104: ViewController.swift:64 (viewDidLoad()) -> subscribed
2017-06-02 15:20:10.112: ViewController.swift:64 (viewDidLoad()) -> Event next(🍎)
🍎
2017-06-02 15:20:10.112: ViewController.swift:64 (viewDidLoad()) -> Event next(🍐)
🍐
2017-06-02 15:20:10.113: ViewController.swift:64 (viewDidLoad()) -> Event next(🍊)
🍊
Error encountered
2017-06-02 15:20:10.115: ViewController.swift:64 (viewDidLoad()) -> Event next(🍎)
🍎
2017-06-02 15:20:10.115: ViewController.swift:64 (viewDidLoad()) -> Event next(🍐)
🍐
2017-06-02 15:20:10.115: ViewController.swift:64 (viewDidLoad()) -> Event next(🍊)
🍊
Error encountered
2017-06-02 15:20:10.115: ViewController.swift:64 (viewDidLoad()) -> Event next(🍎)
🍎
2017-06-02 15:20:10.116: ViewController.swift:64 (viewDidLoad()) -> Event next(🍐)
🍐
2017-06-02 15:20:10.116: ViewController.swift:64 (viewDidLoad()) -> Event next(🍊)
🍊
Error encountered
2017-06-02 15:20:10.120: ViewController.swift:64 (viewDidLoad()) -> Event error(test)
2017-06-02 15:20:10.120: ViewController.swift:64 (viewDidLoad()) -> isDisposed
-
RxSwift.Resources.total
提供所有Rx资源分配的计数,用于开发期间检测泄漏
** 默认使用RxSwift.Resources.total
会报错,需要修改pod文件,修改完毕之后重新pod ,然后编译就可以了**
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
事例程序
example("RxSwift.Resources.total") {
print(RxSwift.Resources.total)
let disposeBag = DisposeBag()
print(RxSwift.Resources.total)
let variable = Variable("🍎")
let subscription1 = variable.asObservable().subscribe(onNext: { print($0) })
print(RxSwift.Resources.total)
let subscription2 = variable.asObservable().subscribe(onNext: { print($0) })
print(RxSwift.Resources.total)
subscription1.dispose()
print(RxSwift.Resources.total)
subscription2.dispose()
print(RxSwift.Resources.total)
}
// 控制台打印的结果:
--- RxSwift.Resources.total example ---
0
2
🍎
8
🍎
10
9
8