iOS崩潰處理機(jī)制:NSNotification Crash

產(chǎn)生的原因:
當(dāng)一個(gè)對象添加了notification之后,如果dealloc的時(shí)候,仍然持有notification,就會(huì)出現(xiàn)NSNotification類型的crash。NSNotification類型的crash多產(chǎn)生于程序員寫代碼時(shí)候犯疏忽,在NSNotificationCenter添加一個(gè)對象為observer之后,忘記了在對象dealloc的時(shí)候移除它。

iOS9之前會(huì)crash,iOS9之后蘋果系統(tǒng)已優(yōu)化。在iOS9之后,即使開發(fā)者沒有移除observer,Notification crash也不會(huì)再產(chǎn)生了。

解決方案:
NSNotification Crash的防護(hù)原理很簡單, 利用method swizzling hook NSObject的dealloc函數(shù),再對象真正dealloc之前先調(diào)用一下:[[NSNotificationCenter defaultCenter] removeObserver:self],即可。

具體方式:

#import <Foundation/Foundation.h>

/**
 當(dāng)一個(gè)對象添加了notification之后,如果dealloc的時(shí)候,仍然持有notification,就會(huì)出現(xiàn)NSNotification類型的crash。
 
 iOS9之后專門針對于這種情況做了處理,所以在iOS9之后,即使開發(fā)者沒有移除observer,Notification crash也不會(huì)再產(chǎn)生了
 */

NS_ASSUME_NONNULL_BEGIN

@interface NSObject (NSNotificationCrash)

+ (void)xz_enableNotificationProtector;

@end

NS_ASSUME_NONNULL_END
#import "NSObject+NSNotificationCrash.h"
#import "NSObject+XZSwizzle.h"
#import <objc/runtime.h>


static const char *isNSNotification = "isNSNotification";

@implementation NSObject (NSNotificationCrash)


+ (void)xz_enableNotificationProtector {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSObject *objc = [[NSObject alloc] init];
        
        [objc xz_instanceSwizzleMethod:@selector(addObserver:selector:name:object:) replaceMethod:@selector(xz_addObserver:selector:name:object:)];
        
        // 在ARC環(huán)境下不能顯示的@selector dealloc。
        [objc xz_instanceSwizzleMethod:NSSelectorFromString(@"dealloc") replaceMethod:NSSelectorFromString(@"xz_dealloc")];
    });
}

- (void)xz_addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject {
    
    // 添加標(biāo)志位,在delloc中只有isNSNotification是YES,才會(huì)移除通知
    [observer setIsNSNotification:YES];
    [self xz_addObserver:observer selector:aSelector name:aName object:anObject];
}


- (void)setIsNSNotification:(BOOL)yesOrNo {
    objc_setAssociatedObject(self, isNSNotification, @(yesOrNo), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isNSNotification {
    NSNumber *number = objc_getAssociatedObject(self, isNSNotification);;
    return  [number boolValue];
}

/**
 如果一個(gè)對象從來沒有添加過通知,那就不要remove操作
 */
- (void)xz_dealloc
{
    if ([self isNSNotification]) {
        NSLog(@"CrashProtector: %@ is dealloc,but NSNotificationCenter Also exsit",self);
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    [self xz_dealloc];
}

@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容