About iOS Kvo

Source: Internet
Author: User

In a complex, stateful system, when the state of an object changes, how to notify the system, and the corresponding behavior to change the state is an issue that needs to be considered, providing 4 workarounds for this type of problem in iOS:

1. Nsnotifiactaion and Nsnotificationcenter: Notification Center
2. Delegates: Agent,
3. Callback: Callback,
4. KVO (Key-value Observing): Key value observation

This article is about using KVO to notify the system of changes in the state of the usage.

What is KVO?  
KVO is a notification mechanism defined in OBJECT-C that defines the mechanism by which a change in the state of an object is monitored and a reaction is made. Objects can register observers for their own properties, and when the value of this property changes, the system notifies those registered observers. Its use is very extensive, for example, your download progress bar is based on the download percentage, then you can see the download percentage changes, refresh the progress bar style, to intuitively respond to the download progress and so on.

the use of KVO 
according to the above description, the use of a kvo mainly involves 3 parts:

1.registering observers for an object's properties: An object adds an observer to a property by calling the following method

Object-c Code

    1. -(void) Addobserver: (NSOBJECT *) Observer

    2. Forkeypath: (NSString *) keypath

    3. Options: (nskeyvalueobservingoptions) options

    4. Context: (void *) context


    • Observer: Viewer object. It must implement method ObserveValueForKeyPath:ofObject:change:context:.

    • KeyPath: The observed attribute, which cannot be nil.

    • Options: Set the value of the attribute passed when the viewer is notified, is it before the change, or after the change, the specific settings can be here: https://developer.apple.com/library/mac/documentation/Cocoa/ reference/foundation/protocols/nskeyvalueobserving_protocol/reference/reference.html#//apple_ref/doc/c_ref/ Nskeyvalueobservingoptions, usually set to Nskeyvalueobservingoptionnew.

    • Context: Some other contextual information that needs to be passed to the observer, usually set to nil



2. The Observer receives the notification and makes the processing: The observer completes the response to the property change by implementing the following method:

Object-c Code

    1. -(void) Observevalueforkeypath: (NSString *) keypath

    2. Ofobject: (ID) object

    3. Change: (nsdictionary *) change

    4. Context: (void *) context



    • KeyPath: The property being observed, which cannot be nil.

    • Object: Objects of the Observer.

    • Change: Property value, based on the options set above, gives the corresponding property value

    • Context: The context object passed above.



3. Clear the Observer: The object removes the Observer by this method:

Object-c Code

    1. -(void) Removeobserver: (NSObject *) anobserver Forkeypath: (NSString *) keypath



4.Precautions : 
There are two requirements for using the KVO messaging mechanism: (1) The observer must know the object being observed, that is, in the same scope. (2) The observer also needs to know the life cycle of the object being observed, because the observer's registration needs to be canceled before the sender object is destroyed.
also: If you plan to use KVO on the core data object, you need to know that this is a bit different from the general KVO usage. It must also be combined with the failure mechanism of core data (faulting mechanism), which, once the core data fails, will trigger the observer for its properties (even if the values of these properties have not changed).

5.some good practices 

    • (1) When an observer observes the same attributes of multiple objects (that is, different object, but KeyPath is the same), the different notifications can be distinguished by setting a static context variable.

    • (2) Use Nsstringfromselector (@selector method) to obtain keypath instead of writing the property name directly through NSString, so that the compiler can help discover typo in the property name.

    • (3) by method: + (Nsset *) Keypathsforvaluesaffectingvalueforkey: (NSString *) key, observe the change of multiple attribute values through a key.



Reference article: http://ningandjiao.iteye.com/blog/2009729

About iOS Kvo

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.