iOS底层探究—KVO

KVO的全称是Key-Value Observing,俗称“键值监听”,可以用于监听某个对象属性值的改变


1.KVO使用方法

1. 添加监听:

addObserver:forKeyPath:options:context:

self.person = [[Person alloc] init];
self.person.age = 1;

NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.person addObserver:self forKeyPath:@"age" options:options context:@"123"];

2. 监听回调:

当监听对象的属性值发生改变时,就会调用observeValueForKeyPath:ofObject:change: context:

// 当监听对象的属性值发生改变时,就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}

2.探索KVO的本质

@interface ViewController ()
@property (strong, nonatomic) Person *person1;
@property (strong, nonatomic) Person *person2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.person1 = [[Person alloc] init];
    self.person1.age = 1;
    
    self.person2 = [[Person alloc] init];
    self.person2.age = 2;
    
    // 给person1对象添加KVO监听
    NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
    [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    self.person1.age = 21;
//    self.person2.age = 22;
    
    // NSKVONotifying_Person是使用Runtime动态创建的一个类,是Person的子类
    
    // self.person1.isa == NSKVONotifying_Person
    [self.person1 setAge:21];
    
    // self.person2.isa = Person
    [self.person2 setAge:22];
}

- (void)dealloc {
    [self.person1 removeObserver:self forKeyPath:@"age"];
}
// 当监听对象的属性值发生改变时,就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}

@end

在上面的代码中,仅仅对person1添加了KVO监听,通过断点分别获取到person1isa->NSKVONotifying_Person,
person2isa->Person

image

当实例对象未使用KVO监听,person1isa指向的类对象为Person,结构如下:
未使用KVO监听

当实例对象添加了KVO监听,person1isa指向的类对象就变成了另外一个NSKVONotifying_Person,而NSKVONotifying_Person类对象的superclass指针才指向了Person
NSKVONotifying_Person是使用Runtime动态创建的一个类,是Person的子类,结构如下:

使用KVO监听

当调用setAge:方法的时候,通过查看IMP地址,实际上调用的是Foundation框架里的_NSSetIntValueAndNotify函数,

调用过程如下(伪代码):

- (void)setAge:(int)age
{
    _NSSetIntValueAndNotify();
}

// 伪代码
void _NSSetIntValueAndNotify()
{
    [self willChangeValueForKey:@"age"];
    [super setAge:age];
    [self didChangeValueForKey:@"age"];
}

- (void)didChangeValueForKey:(NSString *)key
{
    // 通知监听器,某某属性值发生了改变
    [oberser observeValueForKeyPath:key ofObject:self change:nil context:nil];
}

_NSSetIntValueAndNotify函数里面先是调用了[self willChangeValueForKey:@"age"] -> [super setAge:age]->[self didChangeValueForKey:@"age"];,然后在didChangeValueForKey:里面通过监听对象[oberser observeValueForKeyPath:key ofObject:self change:nil context:nil]


3.查看_NSSet*AndNotify的存在

根据对监听对象类型的不同,生成的方法也不同


4._NSSet*ValueAndNotify的内部实现

NSSet*ValueAndNotify的内部实现
  • 调用willChangeValueForKey:
  • 调用原来的setter实现
  • 调用didChangeValueForKey:
  • didChangeValueForKey:内部会调用observer的observeValueForKeyPath:ofObject:change:context:方法

你可能感兴趣的:(iOS底层探究—KVO)