An overview of the
KVO, which stands for KeyValueObserving, is an event notification mechanism provided by Apple. Allows an object to listen for changes to specific properties of another object and receive events when they change. Properties work because of the implementation mechanism of KVO, which is generally supported by objects that inherit from NSObject.
Both KVO and NSNotificationCenter are implementations of the Observer pattern in iOS. The difference is that KVO is one-to-one, not one-to-many, relative to the relationship between observed and observer. KVO is non-intrusive to the monitored object and can be monitored without modifying its internal code.
KVO can listen for changes in individual properties as well as collection objects. The proxy object is obtained through methods such as mutableArrayValueForKey: of KVC. When the internal object of the proxy object changes, the method monitored by KVO is called back. The collection object contains NSArray and NSSet.
You may often encounter such an interview question:
How does iOS implement KVO for an object? (What is the nature of KVO?)
(If you haven’t read my previous blog, or if you don’t know anything about oc objects, I suggest you read it first, on my blog a few days ago at ****, otherwise this post is a bit confusing.)
Knowledge review:
First let’s review what we know about KVO
This is the more common kvo classic implement as we know, the change of value was introduced into the option of a certain relationship with you, our common general is introduced into NSKeyValueObservingOptionOld and NSKeyValueObservingOptionNew, A new value and an old value, so change will print something like this, and context is the value that you put in when you listen, and when you print something, whatever value you pass in will print.
Another interview question asks: Does directly changing the value of a variable trigger KVO?
Answer: KVO does not trigger
As you can see from the screenshot above. Self. Person1 call setAge will trigger the – (void) when observeValueForKeyPath… This way! Person1 and person2 both call setAge, and the code is the same. How can one trigger the setAge method and the other not?
Kvo call process:
As we know before: The isa for person2 isa class object that points to GDPerson, storing isa, superclass, setage, age, etc., so person2 will find the GDPerson class object through isa and call the setage method inside. This is clear
Let’s start with a screenshot
As you can see from the screenshot
The class object for Person1 is NSKVONotifying_GDPerson, which is a class created dynamically using the Runtime
Self. Person1. Isa = NSKVONotifying_GDPerson;
Kvo calls a c function called Foundation ‘_NSSetIntValueAndNotify’. The code inside is invisible. You can write some pseudo-code as follows:
Mainly for doing so many things!
Verify the call to Foundation ‘_NSSetIntValueAndNotify’
So there’s a method in Runtime that we can use to see the implementation of this method :<#(SEL)#>
Next I will use code to prove: kVO derived class NSKVONotifying_GDPerson in the specific existence of class information, continue to elaborate on the nature of this KVO, and make a summary.