作为
RxSwift
四大才子之一,销毁者dispose
扮演者举足轻重(灰常重要)的作用
上代码,序列的创建到销毁
// 创建序列
let ob = Observable.create { (observer) -> Disposable in
observer.onNext("Episode:阿拉丁爱吃鱼")
return Disposables.create { print("销毁释放了")}
}
// 序列订阅
let dispose = ob.subscribe(onNext: { (anything) in
print("订阅到了: \(anything)")
}, onError: { (error) in
print("订阅到了: \(error)")
}, onCompleted: {
print("完成了")
}) {
print("销毁回调")
}
// status - 算法 - dispose 随时随地都能销毁
print("执行完毕")
dispose.dispose()
订阅到了: Episode:阿拉丁爱吃鱼
执行完毕
销毁释放了
销毁回调
销毁者Disposables
的源码
extension Disposables {
public static func create(with dispose: @escaping () -> Void) -> Cancelable {
return AnonymousDisposable(disposeAction: dispose)
}
}
1.Disposables.create { print("销毁释放了")}
实质上创建了一个AnonymousDisposable
类型的销毁者对象
2.传入了业务层的监听dispose
闭包,销毁对象时用来通知
fileprivate final class AnonymousDisposable : DisposeBase, Cancelable {
fileprivate init(_ disposeAction: @escaping DisposeAction) {
self._disposeAction = disposeAction
super.init()
}
// 核心代码
fileprivate func dispose() {
if fetchOr(self._isDisposed, 1) == 0 {
if let action = self._disposeAction {
self._disposeAction = nil
action()
}
}
}
}
AnonymousDisposable
遵循了Cancelable
协议
public protocol Cancelable : Disposable {
var isDisposed: Bool { get }
}
Cancelable
又遵循了Disposable
协议
public protocol Disposable {
func dispose()
}
dispose()
为销毁方法,该方法调用后就销毁了传入的闭包对象,就是最初create的那个
fetchOr(self._isDisposed, 1)
这个函数呢
oldValue
,就是为了保证只有第一次的值为0,后面再次执行就为1,酱紫保证了{}里的方法只走一次
那么问题来了,这个
dispose()
方法到底是神马时候调用的呢
public func subscribe(onNext: ((Element) -> Void)? = nil, onError: ((Swift.Error) -> Void)? = nil, onCompleted: (() -> Void)? = nil, onDisposed: (() -> Void)? = nil)
...
-> Disposable {
let observer = AnonymousObserver { event in
switch event {
case .next(let value):
onNext?(value)
case .error(let error):
if let onError = onError {
onError(error)
}
else {
Hooks.defaultErrorHandler(callStack, error)
}
disposable.dispose()
case .completed:
onCompleted?()
disposable.dispose()
}
}
return Disposables.create(
self.asObservable().subscribe(observer),
disposable
)
}
}
so,原来实是在 .error
或者.completed
的时候,调用了 disposable.dispose()
额,那么onNex
呢,不禁看到了Disposables.create
return Disposables.create(
self.asObservable().subscribe(observer),
disposable
)
看看这个create
的实现
extension Disposables {
public static func create(_ disposable1: Disposable, _ disposable2: Disposable) -> Cancelable {
return BinaryDisposable(disposable1, disposable2)
}
}
private final class BinaryDisposable : DisposeBase, Cancelable {
func dispose() {
if fetchOr(self._isDisposed, 1) == 0 {
self._disposable1?.dispose()
self._disposable2?.dispose()
self._disposable1 = nil
self._disposable2 = nil
}
}
}
元祖里的两个对象分别进行了销毁
接下来,我们来看看
Producer
和拥有着RxSwift
世界的Sink
class Producer : Observable {
override func subscribe(_ observer: Observer) -> Disposable where Observer.Element == Element {
let disposer = SinkDisposer()
let sinkAndSubscription = self.run(observer, cancel: disposer)
disposer.setSinkAndSubscription(sink: sinkAndSubscription.sink, subscription: sinkAndSubscription.subscription)
return disposer
}
}
}
disposer.setSinkAndSubscription
fileprivate final class SinkDisposer: Cancelable {
func setSinkAndSubscription(sink: Disposable, subscription: Disposable) {
self._sink = sink
self._subscription = subscription
if (previousState & DisposeState.disposed.rawValue) != 0 {
sink.dispose()
subscription.dispose()
self._sink = nil
self._subscription = nil
}
}
我们来看看AnonymousObservableSink
,这个连接着序列和观察者的销毁
final private class AnonymousObservableSink: Sink, ObserverType {
func on(_ event: Event) {
switch event {
case .next:
if load(self._isStopped) == 1 {
return
}
self.forwardOn(event)
case .error, .completed:
if fetchOr(self._isStopped, 1) == 0 {
self.forwardOn(event)
self.dispose()
}
}
}
so,当订阅序列遇到.error
, .completed
的时候self.dispose()
,咔咔。。。
sink
本身是Disposable
对象,被销毁后就断开了序列和订阅者之间的联系。