func deallocDemo() {
_ = rx.deallocating.subscribe(onNext :{() in
print("nihao")
} )
}
走到deallocating的getter方法
public var deallocating: Observable<()> {
return self.synchronized {
do {
let proxy: DeallocatingProxy = try self.registerMessageInterceptor(deallocSelector)
return proxy.messageSent.asObservable()
}
catch let e {
return Observable.error(e)
}
}
}
dealloc來自Reactive的擴展extension Reactive where Base: AnyObject針對于NSObject,所以只要是NSObject都支持deallocating方法。
public var deallocating: Observable<()> {
return self.synchronized {
do {
let proxy: DeallocatingProxy = try self.registerMessageInterceptor(deallocSelector)
return proxy.messageSent.asObservable()
}
catch let e {
return Observable.error(e)
}
}
}
proxyclass類型由self.registerMessageInterceptor(deallocSelector)① 創(chuàng)建序列返回proxy.messageSent.asObservable()。messageSent發(fā)送序列
@objc func deallocating() {
self.messageSent.on(.next(()))//③*發(fā)送序列*
}
deallocSelector是private let deallocSelector = NSSelectorFromString("dealloc")``dealloc方法
這里需要注意:為啥要通過string來拿dealloc方法?
因為dealloc方法無法通過@selector進行重寫。系統(tǒng)的析構函數(shù)不允許重寫。所以通過string進行獲取。
在_ = rx.deallocating.subscribe進行序列的②訂閱
①②③是序列的基本流程。

問題:怎么交換方法?swizzing
let targetImplementation = RX_ensure_observing(self.base, selector, &error)ensure_observing重點在這。添加了對dealloc的觀察。來到里面看到
[[RXObjCRuntime instance] performLocked:^(RXObjCRuntime * __nonnull self) {
targetImplementation = [self ensurePrepared:target
forObserving:selector
error:error];
}];
RXObjCRuntime來到OC的runtime。通過performLocked加鎖安全處理。
-(void)performLocked:(void (^)(RXObjCRuntime* __nonnull))action {
pthread_mutex_lock(&_lock);
action(self);
pthread_mutex_unlock(&_lock);
}
來到ensurePrepared方法找到了swizzleDeallocating,點擊進入
SWIZZLE_INFRASTRUCTURE_METHOD(
void,
swizzleDeallocating,
,
deallocSelector,
DEALLOCATING_BODY
)
點擊進入宏定義SWIZZLE_INFRASTRUCTURE_METHOD里面是實現(xiàn)交換方法的代碼。被宏定義了。
#define SWIZZLE_INFRASTRUCTURE_METHOD(return_value, method_name, parameters, method_selector, body, ...) \
SWIZZLE_METHOD(return_value, -(BOOL)method_name:(Class __nonnull)class parameters error:(NSErrorParam)error \
{ \
SEL selector = method_selector; , body, NO_BODY, __VA_ARGS__) \
// common base
#define SWIZZLE_METHOD(return_value, method_prototype, body, invoked_body, ...) \
method_prototype \
__unused SEL rxSelector = RX_selector(selector); \
IMP (^newImplementationGenerator)(void) = ^() { \
__block IMP thisIMP = nil; \
id newImplementation = ^return_value(__unsafe_unretained id self DECLARE_ARGUMENTS(__VA_ARGS__)) { \
body(__VA_ARGS__) \
\
struct objc_super superInfo = { \
.receiver = self, \
.super_class = class_getSuperclass(class) \
}; \
\
return_value (*msgSend)(struct objc_super *, SEL DECLARE_ARGUMENTS(__VA_ARGS__)) \
= (__typeof__(msgSend))objc_msgSendSuper; \
@try { \
return msgSend(&superInfo, selector ARGUMENTS(__VA_ARGS__)); \
} \
@finally { invoked_body(__VA_ARGS__) } \
}; \
\
thisIMP = imp_implementationWithBlock(newImplementation); \
return thisIMP; \
}; \
\
IMP (^replacementImplementationGenerator)(IMP) = ^(IMP originalImplementation) { \
__block return_value (*originalImplementationTyped)(__unsafe_unretained id, SEL DECLARE_ARGUMENTS(__VA_ARGS__) ) \
= (__typeof__(originalImplementationTyped))(originalImplementation); \
\
__block IMP thisIMP = nil; \
id implementationReplacement = ^return_value(__unsafe_unretained id self DECLARE_ARGUMENTS(__VA_ARGS__) ) { \
body(__VA_ARGS__) \
@try { \
return originalImplementationTyped(self, selector ARGUMENTS(__VA_ARGS__)); \
} \
@finally { invoked_body(__VA_ARGS__) } \
}; \
\
thisIMP = imp_implementationWithBlock(implementationReplacement); \
return thisIMP; \
}; \
\
return [self ensureSwizzledSelector:selector \
ofClass:class \
newImplementationGenerator:newImplementationGenerator \
replacementImplementationGenerator:replacementImplementationGenerator \
error:error]; \
} \
去掉“\”之后就是實現(xiàn)的代碼了。通過ensureSwizzledSelector方法進行交換。
-(BOOL)ensureSwizzledSelector:(SEL __nonnull)selector
ofClass:(Class __nonnull)class
newImplementationGenerator:(IMP(^)(void))newImplementationGenerator
replacementImplementationGenerator:(IMP (^)(IMP originalImplementation))replacementImplementationGenerator
error:(NSErrorParam)error {
if ([self interceptorImplementationForSelector:selector forClass:class] != nil) {
DLOG(@"Trying to register same intercept at least once, this sounds like a possible bug");
return YES;
通過IMP進行兩者的互換。
原因是1:因為swift特性,無法動態(tài)的編譯。達到預編譯的效果。
2:速度快。
問題:什么時候調用的deallocating?
在
#define DEALLOCATING_BODY(...) \
id<RXDeallocatingObserver> observer = objc_getAssociatedObject(self, rxSelector); \
if (observer != nil && observer.targetImplementation == thisIMP) { \
[observer deallocating]; //這里調用了 deallocating \
}