產(chǎn)生的原因:
當一個對象添加了notification之后话原,如果dealloc的時候,仍然持有notification,就會出現(xiàn)NSNotification類型的crash或南。NSNotification類型的crash多產(chǎn)生于程序員寫代碼時候犯疏忽孩等,在NSNotificationCenter添加一個對象為observer之后,忘記了在對象dealloc的時候移除它迎献。
iOS9之前會crash瞎访,iOS9之后蘋果系統(tǒng)已優(yōu)化。在iOS9之后吁恍,即使開發(fā)者沒有移除observer扒秸,Notification crash也不會再產(chǎn)生了。
解決方案:
NSNotification Crash的防護原理很簡單冀瓦, 利用method swizzling hook NSObject的dealloc函數(shù)伴奥,再對象真正dealloc之前先調(diào)用一下:[[NSNotificationCenter defaultCenter] removeObserver:self],即可翼闽。
具體方式:
#import <Foundation/Foundation.h>
/**
當一個對象添加了notification之后拾徙,如果dealloc的時候,仍然持有notification感局,就會出現(xiàn)NSNotification類型的crash尼啡。
iOS9之后專門針對于這種情況做了處理,所以在iOS9之后询微,即使開發(fā)者沒有移除observer崖瞭,Notification crash也不會再產(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 {
// 添加標志位撑毛,在delloc中只有isNSNotification是YES书聚,才會移除通知
[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];
}
/**
如果一個對象從來沒有添加過通知,那就不要remove操作
*/
- (void)xz_dealloc
{
if ([self isNSNotification]) {
NSLog(@"CrashProtector: %@ is dealloc藻雌,but NSNotificationCenter Also exsit",self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
[self xz_dealloc];
}
@end