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.
-
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.
For example, a
PersonObject
will want to be aware of any changes made to theiraccountBalance
in theBankObject
. -
The
PersonObject
must register as an observer of theBankObject
’saccountBalance
property by sending anaddObserver:forKeyPath:options:context:
message. -
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. -
The
observeValueForKeyPath:ofObject:change:context:
method is automatically invoked when the value of an observed property is changed in a KVO-compliant manner.
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.