KVO实现原理
创建一个类声明一个属性并且给这个属性添加监听
- (void)viewDidLoad {
[super viewDidLoad];
self.teacher1 = [[Teacher alloc] init];
self.teacher1.age = 30;
self.teacher2 = [[Teacher alloc] init];
self.teacher2.age = 50;
// 给teacher1对象添加KVO监听
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.teacher1 addObserver:self forKeyPath:@"age" options:options context:@"teacher"];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.teacher1 setAge:25];
[self.teacher2 setAge:55];
}
- (void)dealloc {
[self.teacher1 removeObserver:self forKeyPath:@"age"];
}
// 当监听对象的属性值发生改变时,就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}
在set方法中打断点不难发现当程序运行teacher2的时候并没有出发KVO,都是调用set方法,为何没有添加监听者的属性改变了却没有触发,那是如何实现的呢?
两个方法都是执行的set方法,所以差异性就会体现在teacher1和teacher2身上。
接下来打印teacher1和teacher2的isa指针就会发现
(Class) $1 = NSKVONotifying_Teacher
Fix-it applied, fixed expression was:
self.teacher1->isa
(Class) $2 = Teacher
Fix-it applied, fixed expression was:
self.teacher2->isa
teacher的isa指向NSKVONotifying_Teacher
所以当你给对象添加KVO之后isa指针就不会指向原来的类而是NSKVONotifying_XXX。
可以来验证一下
创建一个NSKVONotifying_Teacher类之后会报如下的错误
KVO failed to allocate class pair for name NSKVONotifying_Teacher, automatic key-value observing will not work for this class
NSKVONotifying_Teacher是使用Runtime动态创建的一个类,是Teacher的子类
再来验证一下
NSLog(@"teacher1添加KVO监听之前 - %@ %@",
object_getClass(self.teacher1),
object_getClass(self.teacher2));
// 给teacher1对象添加KVO监听
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.teacher1 addObserver:self forKeyPath:@"age" options:options context:@"teacher"];
NSLog(@"teacher1添加KVO监听之后 - %@ %@",
object_getClass(self.teacher1),
object_getClass(self.teacher2));
teacher1添加KVO监听之前 - Teacher Teacher
teacher1添加KVO监听之后 - NSKVONotifying_Teacher Teacher
当调用set方法时teacher1的isa指针回去类对象中寻找方法,因为此时添加了监听所以回去NSKVONotifying_Teacher寻找set方法,由此也能得出结论这个NSKVONotifying_Teacher类里面同样有set方法。
验证一下
NSLog(@"teacher1添加KVO监听之前 - %p %p",
[self.teacher1 methodForSelector:@selector(setAge:)],
[self.teacher2 methodForSelector:@selector(setAge:)]);
// 给teacher1对象添加KVO监听
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.teacher1 addObserver:self forKeyPath:@"age" options:options context:@"teacher"];
NSLog(@"teacher1添加KVO监听之后 - %p %p",
[self.teacher1 methodForSelector:@selector(setAge:)],
[self.teacher2 methodForSelector:@selector(setAge:)]);
teacher1添加KVO监听之前 - 0x10d3c87a0 0x10d3c87a0
teacher1添加KVO监听之后 - 0x10d721cf2 0x10d3c87a0
由此发现set方法的地址变化了
通过上面的地址我们可以得到这样一个结果
(lldb) p (IMP)0x10d3c87a0
(IMP) $0 = 0x000000010d3c87a0 (Interview01`-[Teacher setAge:] at Teacher.h:15)
(lldb) p (IMP)0x10d721cf2
(IMP) $1 = 0x000000010d721cf2 (Foundation`_NSSetIntValueAndNotify)
由此发现NSKVONotifying_Teacher调用的是_NSSetIntValueAndNotify()(因为我声明的age是int类型)其内部实现可能是
[self willChangeValueForKey:@"age"];
[super setAge:age];
[self didChangeValueForKey:@"age"];
那么KVO的内部调用流程是怎样的呢,重写
setAge:
willChangeValueForKey:
didChangeValueForKey:
打印一下看看打印顺序
- (void)setAge:(int)age
{
_age = age;
NSLog(@"%s",__func__);
}
- (void)willChangeValueForKey:(NSString *)key
{
[super willChangeValueForKey:key];
NSLog(@"%s",__func__);
}
- (void)didChangeValueForKey:(NSString *)key
{
NSLog(@"%s --- begin",__func__);
[super didChangeValueForKey:key];
NSLog(@"%s --- end",__func__);
}
- 打印结果
[22980:1320954] -[Teacher willChangeValueForKey:]
[22980:1320954] -[Teacher setAge:]
[22980:1320954] -[Teacher didChangeValueForKey:] --- begin
[22980:1320954] 监听到<Teacher: 0x6000027683a0>的age属性值改变了 - {
kind = 1;
new = 25;
old = 30;
} - teacher
[22980:1320954] -[Teacher didChangeValueForKey:] --- end
其实到这里可以发现KVO在运行时会动态生成一个类,在实现某些功能时代理的效率会比KVO要高