现在的位置: 首页 > 综合 > 正文

KVO Quick Start

2013年09月12日 ⁄ 综合 ⁄ 共 3399字 ⁄ 字号 评论关闭
 

Key-Value Observing Quick Start

You should read this article to quickly gain an understanding of the key-value observing mechanism. It provides an overview of what key-value observing is, explains why and when it should be used, and briefly describes how it works. You must have a basic understanding of key-value coding before reading this article.

Overview of the Key-Value Observing Mechanism

Key-Value Observing (KVO for short) is a mechanism that allows objects to be notified of changes to specific properties of other objects. A simple KVO mechanism could consist of an observing object and an observed object. Every time a specified property in the observed object changes, it automatically notifies the observing object, which can react accordingly.

The Benefits of KVO

KVO provides a standardized, and oftentimes automatic, infrastructure for posting change notifications. This infrastructure has a couple of benefits. First, you won’t have to implement your own scheme to send notifications every time a property changes. This is the largest benefit this mechanism provides, as it’s well-defined infrastructure has framework-level support that makes it easy to adopt. You will not have to add any code to your project, or design your own scheme for observing specific properties in objects.

Second, the infrastructure is already full-featured, which makes it easy to support multiple observers for a single property, as well as dependent values.

When to Use KVO

The KVO mechanism is used anytime there is an object that needs to be notified of changes made to another object. For example, there could be a view that is displaying the data of a model object. The view, in order to make sure it is displaying the most up-to-date data, needs to be notified when the model object’s data changes. In this scenario, the model is the observed object and the view is the observer. Similarly, the KVO mechanism is also useful when working with dependent variables. A dependent variable is a variable whose value depends on the value of another variable. For example, afullName property may depend upon firstName and lastName properties. The KVO mechanism allows the fullName property to receive an alert every time the firstName or lastName properties change.

How It Works

There are three steps to setting up an observer of a property. Understanding these three steps provides a clear illustration of how the KVO design works.

  1. First, see whether you have a scenario where key-value observing could be beneficial, for example, an object that needs to be notified when any changes are made to a specific property in another object.

    image: ../Art/kvo_objects.jpg

    For example, a PersonObject will want to be aware of any changes made to their accountBalance in the BankObject.

  2. The PersonObject must register as an observer of the BankObject’s accountBalance property by sending an addObserver:forKeyPath:options:context: message.

    image: ../Art/kvo_objects_connection.jpg

    Note: The addObserver:forKeyPath:options:context: method establishes a connection between the instances of the objects that you specify. A connection is not established between the two classes, but rather between the two specified instances of the objects.

  3. In order to respond to change notifications, the observer must implement the observeValueForKeyPath:ofObject:change:context: method. This method implementation defines how the observer responds to change notifications. It is in this method that you can customize your response to a change in one of the observed properties.

    image: ../Art/kvo_objects_implementation.jpg
  4. The observeValueForKeyPath:ofObject:change:context: method is automatically invoked when the value of an observed property is changed in a KVO-compliant manner.

    image: ../Art/kvo_objects_notification.jpg

What’s Next

This article provided just a brief overview of the KVO mechanism. There are many details, such as automatic and manual key-value observing, or to-many and to-one relationships, all of which contribute to the powerful, yet flexible, nature of the KVO mechanism. See the rest of this programming guide for details on these topics.

抱歉!评论已关闭.