之前在探索RxSwift源码的时候,发现几个地方都有循环引用,但那是故意的,而我们使用RxSwift的时候,如果循环引用就不好说故意了。那现在根据RxSwift核心逻辑和销毁流程的探索,把RxSwift的引用关系捋一捋:
1. Observable<Any>.create{ self }
self.observable = Observable<Any>.create { (anyObserver) -> Disposable in
print(self) //循环引用1
anyObserver.onNext("保存了observable")
anyObserver.onCompleted()
return Disposables.create()
}
- 引用关系
self
->
AnonymousObservable
->
SubscribeHandler
->
self
所以如果不保存observable
是没有循环引用的:
let observable = Observable<Any>.create { (anyObserver) -> Disposable in
print(self) //没有循环引用
anyObserver.onNext("不保存observable")
anyObserver.onCompleted()
return Disposables.create()
}
2. observable.subscribe { self }
let observable = Observable<Any>.create { (anyObserver) -> Disposable in
anyObserver.onNext("没有使用weak")
anyObserver.onCompleted()
return Disposables.create()
}
let dispose = observable.subscribe { (text) in
print(self) //循环引用2
}
dispose.disposed(by: self.disposbag)
- 引用关系
self
->
DisposeBag
->
BinaryDisposable
->
SinkDisposer
->
AnonymousObservableSink
->
AnonymousObserver
->
EventHandler
->
onNext
->
self
所以使用时要用weak
:
let observable = Observable<Any>.create { (anyObserver) -> Disposable in
anyObserver.onNext("使用weak修饰")
anyObserver.onCompleted()
return Disposables.create()
}
let dispose = observable.subscribe {[weak self] (text) in
print(self) //没有循环引用
}
dispose.disposed(by: self.disposbag)
3. Disposables.create{ self }
第一种情况
self.observable = Observable<Any>.create { (anyObserver) -> Disposable in
anyObserver.onNext("保存了observable")
anyObserver.onCompleted()
return Disposables.create{
print(self) //循环引用
}
}
- 引用关系
self
->
AnonymousObservable
->
SubscribeHandler
->
AnonymousDisposable
->
DisposeAction
->
self
所以使用时可以不保存observable
或者使用weak
:
let observable = Observable<Any>.create { (anyObserver) -> Disposable in
anyObserver.onNext("没有保存observable")
anyObserver.onCompleted()
return Disposables.create{
print(self) //没有循环引用
}
}
self.observable = Observable<Any>.create {[weak self] (anyObserver) -> Disposable in
anyObserver.onNext("使用weak修饰")
anyObserver.onCompleted()
return Disposables.create{
print(self) //没有循环引用
}
}
第二种情况
let observable = Observable<Any>.create { (anyObserver) -> Disposable in
anyObserver.onNext("不发送完成事件")
return Disposables.create{
print(self) //循环引用
}
}
let dispose = observable.subscribe { (text) in
print("收到响应")
}
dispose.disposed(by: self.disposbag)
- 引用关系
self
->
DisposeBag
->
BinaryDisposable
->
SinkDisposer
->
AnonymousDisposable
->
DisposeAction
->
self
想要释放要发送完成事件:
let observable = Observable<Any>.create { (anyObserver) -> Disposable in
anyObserver.onNext("发送完成事件")
anyObserver.onCompleted()
return Disposables.create{
print(self) //没有循环引用
}
}
let dispose = observable.subscribe { (text) in
print("收到响应")
}
dispose.disposed(by: self.disposbag)
补充
- 有时项目复杂起来很难找到哪里出问题,为此RxSwift有自己的引用计数:
使用之前,需要先在podfile上添加并执行pod install:
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
接着直接获取现有计数,来判断是否释放完全,如果释放完全计数为0:
RxSwift.Resources.total