Recently, I prepared for the interview and summarized some KVO questions. The answers can be found in the following blogs.

1. What is KVO?

A key-value Observer that allows an object to listen for changes in the properties of other objects and to trigger events when such changes occur. Objects that inherit from NSObject support KVO by default.

2. Basic use:

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; // Do any additional setup after loading the view. //keypath: corresponding attributes. / / NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld will give you the old values and new values are returned, call a NSKeyValueObservingOptionInitial will finish the registration immediately. Context: Any type of value can be received when a callback is received. self.per = [Person new]; [self.per addObserver:selfforKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"zmodo"];
}

- (void)viewDidAppear:(BOOL)animated{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setValue:@"kvo" forKeyPath:@"per.name"];
    });
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"keypath:%@-object:%@-change:%@-context:%@",keyPath,object,change,context);
    keypath:name-object:<Person: 0x6000029fc570>-change:{
    kind = 1;
    new = kvo;
    old = "<null>";
}-context:zmodo

}
Copy the code

<1> Add observer, no strong reference

<2> To remove observers, generally add and remove should be in pairs, otherwise it is easy to crash.

3. When will KVO be triggered?

When a setter method for a property is called, or when a property value is set using KVC

4. How to manually trigger?

Both calls [self willChangeValueForKey:@”balance”] and didChangeValueForKey trigger KVO.

5. How do I disable KVO?

In + (BOOL) automaticallyNotifiesObserversForKey: (theKey nsstrings *) method set up separately.

6. Does setting the value of a private variable trigger KVO? will

7. What is the implementation principle of KVO?

The implementation of KVO relies mainly on Runtime. For the class being observed, the system dynamically generates an intermediate class named Knofiy_Class, which isa subclass of the original class, points the object’s isa pointer to it, and overrides the setter method for the property in that class. The willChangeValueForKey method is called before the value is modified and didChangeValueForKey is called after the value is modified.

8. What are the disadvantages of KVO?

Property name modified after keypath Forgetting to modify can easily result in such NSUnknownKeyException crashing.

Syntax is scattered.

9. Problems?

<1>. An attribute is also observed in the parent class, and in the child class, which overwrites the parent class.

If you call the methods of the parent class in a subclass, and add judgments to both the parent class and the subclass, note that when you receive notification of a property change in the parent class, the received object is also a subclass object.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    if (object == self && [keyPath isEqualToString:@"person"]) {
        NSLog(@"CHildkeypath:%@-object:%@-change:%@-context:%@",keyPath,object,change,context);

    }else{
//        NSLog(@"keypath:%@-object:%@-change:%@-context:%@",keyPath,object,change,context); }}Copy the code

<2> Multiple removeObserver crashes. www.jianshu.com/p/e68b48f0c…

<3>. Does multiple add receive multiple notifications? will

<4>. Attribute changed, keypath forgot to modify, how to solve.

9. Recommend KVOController, an open source framework from Facebook.

It would be nice if you could explain how this works in the interview.

Refer to the link: www.jianshu.com/p/badf5cac0… Tech.glowing.com/cn/implemen… www.jianshu.com/p/badf5cac0…