KVO-understanding and simple use, KVO-understanding and simple use

Source: Internet
Author: User

KVO-understanding and simple use, KVO-understanding and simple use
KVO is the key value observing (key value listening/Observer mode)/is a callback mechanism observer mode: A target object manages all the observer objects dependent on IT/and actively notifies the observer object when its own status changes/this brake notification is usually done by calling the interface methods provided by each observer object the/Observer mode can perfectly decouple the target object from the observer object. Simply put: after an object registers as the listener/When the Monitored object changes/the object sends a notification to the listener/so that the listener executes the callback. This means that the attribute of the specified observed object is modified each time. then/KVO will automatically notify the observer KVO and KVC who respond to the message, both of which depend on the dynamic and timely Runtime./both belong to key-value programming, and the underlying implementation mechanism is isa-swizzling KVO: for example, listen to the contentOffset attribute/of scrollView to dynamically change the attributes of some controls to achieve the effect (including gradient navigation bar/pull-down refresh control and other effects). KVO uses: requirement-the object must support the KVC mechanism (NSobject subclasses support it)/(that is, the value is assigned through setter or KVC) method-register the attributes of the specified observer/implement callback method/remove observation applicability-very suitable model attributes are modified/changes to the UIView/When the attribute value is changed/the listener object will get a notification immediately when you need to detect changes in the attribute values of other classes/classes that do not want to be observed know a bit like the FBI monitor suspect/this time you can use KVO's apple documentation to describe KVO: automatic key-value observing is implemented using a technique called isa-swizzling... when an observer is registered for an attribute of an object the isa pointer of the observed object is modified, pointing to an intermediate class rather than at the true class .. that is to say, the technology he uses is ISA-swizzling) /After we register a listener for an object, the/compiler will modify the ISA pointer of the observed object./It may also modify some attributes of the Monitored object./It points to an intermediate class instead the real class/in a certain sense/This is a scam system to implement KVO steps:

  • When the object of Class A is observed for the first time, the system dynamically creates A derived class of Class A at runtime. We call it B (NSKVONotifying_A ).
  • In the derived class B (NSKVONotifying_A), rewrite the setter method of Class A, and implement the notification mechanism in the rewritten setter method.
  • In this process, the isa pointer of the observed object is directed to the original Class A, and the KVO mechanism is changed to the Class B (NSKVONotifying_A) to implement the listener for changing the attribute values of the current class.
  • Class B (NSKVONotifying_A) will overwrite the class method and disguise itself as class. Class B also overwrites the dealloc method to release resources.
  • The system points all isa pointers to Class A objects to Class B objects.
KVC is the same as KVO/implemented through isa-swizzling technology/When the observer is registered as an object's attribute, the isa pointer of the observed object is modified (that is, when the observer is registered as the observed object's observed object? The observed object of an object's property, and the ISA pointer of this property is modified) /point to an intermediate class/instead of a real class/the result is that the value of the isa pointer does not necessarily reflect the actual class of the Instance/so you cannot rely on the isa pointer to determine whether the object is a member of a class/ use the class Method to Determine the class ISA pointer of the object instance: is a pointer to the Class/(the terminology is to point to the Meta Class pointer to the metaclass to point to the Class type) /We can get this value through the object_getClass method./normally, the internal implementation of the class method is to get the metaclass represented by this ISA pointer) /However, In the kvo mechanism, the/Apple registers the listening object and dynamically creates a new class and metadata through object_allocateClassPair./In this case, object_getClass () it is not the Meta class/that the original ISA points to, but the role of the newly created meta-class ISA pointer: each object has an ISA pointer. Class pointing to this object/he tells the Runtime system what the class of this object is/so when the object is registered as an observer/isa Pointer Points to the new class/then the object to be observed becomes a new magic subclass object (or instance) so the call to setting on this object will call the rewritten setter/to activate the key-value notification mechanism and rewrite the setter method: the new class overrides the setter method Resolution: KVO's key value observation notification relies on NSObject's two methods: willChangeValueForKey/notify. Before the observed attribute changes, willChangeValueForKey is called./notification system, when the keyPath attribute value changes, didChangeValueForKey is called./notification -addObserver: forKeyPath: options: context will also be called and overwritten to observe the genus Setter method. This Inheritance Method injection is implemented at runtime rather than during compilation (options is usually passed-NSkeyValueObservingOptionNew | optional). option is a common parameter in KVO. NSkeyValueObservingOption enumeration: NSkeyValueObservingOptionNew: Provide the value NSkeyValueObservingOptionOld before the change: Provide the changed value NSkeyValueObservingOptionInitial: Observe the initial value (a trigger method is called when the observation service is registered) else: trigger method before and after value Modification (that is, one modification and two triggers) registration (which one needs to register the KVO mechanism/observed attribute values/to observe the selection of key value changes/to facilitate the transmission of the data you need)-addO Bserver: forKeyPath: options: context implementation (callback) (observed attribute value/object ??? /Some changed data is stored, for example, the changed data before the change can be received if the context is not empty at registration.)-observeValueForKeyPath: ofObject: change: context: remove add observation and unobserve appear in pairs, so you need to remove the Observer at the end (you can write the-dealloc method)-removeObserver: forKeyPath:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.