產(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