****** 19.10.30 更新
被观察者在销毁前,要移除所有的观察者,iOS10以下会崩溃,iOS11以上不会崩溃
先上结果
崩溃原因总结
1、observe忘记写监听回调方法 observeValueForKeyPath
2、add和remove次数不匹配
3、监听者和被监听者dealloc之前没有remove(其实也原因2,但是监听者和被监听者的生命周期不同)
KVO 是iOS开发着常用的键值模式,但是使用不好经常会带来崩溃。在复杂的业务逻辑中遇到KVO相关的偶发崩溃,原因寻找起来比较困难(必现的的还好),下面将由简到繁介绍KVO的崩溃情形和崩溃原因。情形六有助于更好的了解kvo的崩溃。
情型一、obsever没有实现observeValueForKeyPath方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
[self.titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
_titleLabel.backgroundColor = [UIColor blueColor];
}
情型二、没有移除KVO 比如observer是VC在pop的时候没有在dealloc中removeKVO
//- (void)dealloc{
// [_titleLabel removeObserver:self //forKeyPath:@"backgroundColor"];
//}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
[self.titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
_titleLabel.backgroundColor = [UIColor blueColor];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"backgroundColor"]) {
NSLog(@"颜色改变了");
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
情型三、多次移除KVO
- (void)dealloc{
//第二次remove
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
_titleLabel.backgroundColor = [UIColor blueColor];
//第一次remove
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"backgroundColor"]) {
NSLog(@"颜色改变了");
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
情型四、多次添加相同KVO但是remove次数不同(如果add 和remove相匹配不会崩溃 例子中在dealloc中remove两次)
- (void)dealloc{
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
// [_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"backgroundColor"]) {
NSLog(@"颜色改变了");
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
情形六 监听者和被监听者的生命周期不同
- (void)dealloc{
[_titleLabel removeObserver:_titleView forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel addObserver:_titleView forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
_titleLabel.backgroundColor = [UIColor blueColor];
///全都注释不会崩溃 任意打开下面一个都会崩溃 这也是一些分业务逻辑复杂的功能,会有不易查出崩溃的原因, 监听对象和被监听对象的释放时机没有掌握好就会导致类似的崩溃。
// 比如对AVPlayerItem 的监听,在切换AVPlayerItem 若果逻辑不严谨可能会导致kvo的崩溃。
//_titleLabel = nil;
// _titleView = nil;
}
///_titleView 自定义的实现 observeValueForKeyPath 方法
#import "ObserView.h"
@implementation ObserView
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"backgroundColor"]) {
NSLog(@"颜色改变了");
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
崩溃原因总结
1、忘记写监听回调方法 observeValueForKeyPath
2、add和remove次数不匹配
3、监听者和被监听者dealloc之前没有remove
解决方案分析
方案一 cc_addObserver 代码在下面
情形一会崩溃
- (void)dealloc{
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel cc_addObserver:self forKeyPath:@"backgroundColor"];
_titleLabel.backgroundColor = [UIColor blueColor];
}
情形二、不会崩溃
- (void)dealloc{
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel cc_addObserver:self forKeyPath:@"backgroundColor"];
_titleLabel.backgroundColor = [UIColor blueColor];
_titleLabel = nil;
}
cc_addObserver 只能防治生命周期不匹配的崩溃形式,不能防止add remove 不匹配的情形
方案二 使用JJException
情形一、不会崩溃
- (void)dealloc{
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
//[_titleLabel removeObserver:self
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
_titleLabel.backgroundColor = [UIColor blueColor];
_titleLabel = nil;
//_titleView = nil;
}
情形二、会崩溃
- (void)dealloc{
[_titleLabel removeObserver: _titleView forKeyPath:@"backgroundColor"];
//[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
_titleLabel = [UILabel new];
_titleLabel.backgroundColor = [UIColor redColor];
_titleLabel.frame = CGRectMake(20, 100, 50, 50);
[self.view addSubview:_titleLabel];
_titleView = [UIView new];
_titleView.backgroundColor = [UIColor yellowColor];
_titleView.frame = CGRectMake(100, 100, 50, 50);
[self.view addSubview:_titleView];
[_titleLabel addObserver:_titleView forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
//_titleLabel
_titleLabel.backgroundColor = [UIColor blueColor];
_titleLabel = nil;
//_titleView = nil;
}
情形三、会崩溃
- (void)dealloc{
[_titleLabel removeObserver: _titleView forKeyPath:@"backgroundColor"];
//[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
_titleLabel = [UILabel new];
_titleLabel.backgroundColor = [UIColor redColor];
_titleLabel.frame = CGRectMake(20, 100, 50, 50);
[self.view addSubview:_titleLabel];
_titleView = [UIView new];
_titleView.backgroundColor = [UIColor yellowColor];
_titleView.frame = CGRectMake(100, 100, 50, 50);
[self.view addSubview:_titleView];
[_titleLabel addObserver:_titleView forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
//_titleLabel
_titleLabel.backgroundColor = [UIColor blueColor];
//_titleLabel = nil;
//_titleView = nil;
}
情形四、不会崩溃
- (void)dealloc{
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
//[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
//_titleLabel
_titleLabel.backgroundColor = [UIColor blueColor];
//_titleLabel = nil;
//_titleView = nil;
}
情形五、不会崩溃
- (void)dealloc{
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
[_titleLabel removeObserver:self forKeyPath:@"backgroundColor"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_titleLabel addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:nil];
//_titleLabel
_titleLabel.backgroundColor = [UIColor blueColor];
//_titleLabel = nil;
//_titleView = nil;
}
总结:
cc_addObserver 和 JJException 都不会完全解决KVO使用不严谨引发的崩溃问题,推荐使用 cc_addObserver,并且不自己写removeObserve的方法,防止监听者为nil时crash
下面是 cc_addObserver 代码
#import <Foundation/Foundation.h>
@interface NSObject (ObserverHelper)
- (void)cc_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
@end
#import "NSObject+ObserverHelper.h"
#import <objc/message.h>
@interface ObserverHelper : NSObject
@property (nonatomic, unsafe_unretained) id target;
@property (nonatomic, unsafe_unretained) id observer;
@property (nonatomic, strong) NSString *keyPath;
@property (nonatomic, weak) ObserverHelper *factor;
@end
@implementation ObserverHelper
- (void)dealloc {
if ( _factor ) {
[_target removeObserver:_observer forKeyPath:_keyPath];
}
}
@end
@implementation NSObject (ObserverHelper)
- (void)cc_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath {
[self addObserver:observer forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:nil];
ObserverHelper *helper = [ObserverHelper new];
ObserverHelper *sub = [ObserverHelper new];
sub.target = helper.target = self;
sub.observer = helper.observer = observer;
sub.keyPath = helper.keyPath = keyPath;
helper.factor = sub;
sub.factor = helper;
const char *helpeKey = [[keyPath mutableCopy] UTF8String];
const char *subKey = [[keyPath mutableCopy] UTF8String];
// 关联属性 举例 self 和 helper 关联 当self释放的时候 helper释放 即可释放self的kvo 观察者和sub关联 当观察者释放的时候 调用sub的移除同样也能删除self的kvo factor是同一个对象 是为防止多次移除导致的崩溃
objc_setAssociatedObject(self, helpeKey, helper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(observer, subKey, sub, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end