RacDealloc交换

SEL deallocSelector = sel_registerName("dealloc");

[[self  rac_signalForSelector:deallocSelector] subscribeNext:^(RACTuple * _Nullable x) {
    NSLog(@"111: %@",x);
} completed:^{
    NSLog(@"completed") ;
}];

[[self  rac_signalForSelector:@selector(aa)] subscribeNext:^(RACTuple * _Nullable x) {
    NSLog(@"12345");
}];

不能同时使用以上的方法。 dealloc就用swizzleDeallocIfNeeded的方法。
其它的普通的Selector方法用NSObjectRACSignalForSelector。

[self.rac_deallocDisposable  addDisposable:[RACDisposable disposableWithBlock:^{
    NSLog(@"11");
}]];
[self.rac_willDeallocSignal subscribeCompleted:^{
    NSLog(@"111");
}];

先执行block里面的方法,再调用主类的dealloc方法。

self.rac_deallocDisposable

  • (RACCompoundDisposable *)rac_deallocDisposable {
    @synchronized (self) {
    RACCompoundDisposable *compoundDisposable = objc_getAssociatedObject(self, RACObjectCompoundDisposable);
    if (compoundDisposable != nil) return compoundDisposable;

      swizzleDeallocIfNeeded(self.class);
    
      compoundDisposable = [RACCompoundDisposable compoundDisposable];
      objc_setAssociatedObject(self, RACObjectCompoundDisposable, compoundDisposable, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
      return compoundDisposable;
    

    }
    }

  • (RACSignal *)rac_willDeallocSignal {
    RACSignal *signal = objc_getAssociatedObject(self, _cmd);
    if (signal != nil) return signal;

    RACReplaySubject *subject = [RACReplaySubject subject];

    [self.rac_deallocDisposable addDisposable:[RACDisposable disposableWithBlock:^{
    // 给当前保存的订阅者发送完成;;; 给新添加的订阅者发送所有的已保存的消息; 如果没有完成, 保存新添加的订阅者。 如果完成了, 就不增加订阅者了。
    [subject sendCompleted];
    }]];

    objc_setAssociatedObject(self, _cmd, subject, OBJC_ASSOCIATION_RETAIN);

    return subject;
    }

static void swizzleDeallocIfNeeded(Class classToSwizzle) {
@synchronized (swizzledClasses()) {
NSString *className = NSStringFromClass(classToSwizzle);
if ([swizzledClasses() containsObject:className]) return;

    SEL deallocSelector = sel_registerName("dealloc");

    __block void (*originalDealloc)(__unsafe_unretained id, SEL) = NULL;

    id newDealloc = ^(__unsafe_unretained id self) {
        RACCompoundDisposable *compoundDisposable = objc_getAssociatedObject(self, RACObjectCompoundDisposable);
        [compoundDisposable dispose];

        if (originalDealloc == NULL) {
            struct objc_super superInfo = {
                .receiver = self,
                .super_class = class_getSuperclass(classToSwizzle)
            };
                // 调用实现了的方法
            void (*msgSend)(struct objc_super *, SEL) = (__typeof__(msgSend))objc_msgSendSuper;
            msgSend(&superInfo, deallocSelector);
        } else {
            originalDealloc(self, deallocSelector);
        }
    };
    
    IMP newDeallocIMP = imp_implementationWithBlock(newDealloc);
    
        // 增加IMP失败, 说明类当前实现了Sel
    if (!class_addMethod(classToSwizzle, deallocSelector, newDeallocIMP, "v@:")) {
        // The class already contains a method implementation.
        Method deallocMethod = class_getInstanceMethod(classToSwizzle, deallocSelector);
        
        // We need to store original implementation before setting new implementation
        // in case method is called at the time of setting.
        originalDealloc = (__typeof__(originalDealloc))method_getImplementation(deallocMethod);
        
            // 方法交换 
        // We need to store original implementation again, in case it just changed.
        originalDealloc = (__typeof__(originalDealloc))method_setImplementation(deallocMethod, newDeallocIMP);
    }

    [swizzledClasses() addObject:className];
}

}

你可能感兴趣的:(RacDealloc交换)