KVO一瞥
Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects. It is particularly useful for communication between model and controller layers in an application. (In OS X, the controller layer binding technology relies heavily on key-value observing.) A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller. In addition, however, a model object may observe other model objects (usually to determine when a dependent value changes) or even itself (again to determine when a dependent value changes).
KVO提供了一种机制,允许对象收到其他对象的特定属性改变的通知。
在应用程序中模型和控制器层之间的通信尤其有用。(在OS X中,控制器层绑定技术严重依赖KVO)。控制器对象通常观察模型对象的属性,视图对象通过控制器观察模型对象的属性。另外,然而,一个模型对象可以观察其他模型对象(通常用来确定依赖值何时改变)甚至自己(再次确定依赖值何时改变)。
You can observe properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made—as well as which objects are involved in the change.
你可以观察属性,包括简单属性,一对一关系和一对多关系。一对多关系的观察者被告知所做出的变化类型以及变化中涉及的对象。
A simple example illustrates how KVO can be useful in your application. Suppose a
Person
object interacts with anAccount
object, representing the person’s savings account at a bank. An instance ofPerson
may need to be aware of when certain aspects of theAccount
instance change, such as the balance, or the interest rate.
一个简单的例子说明了KVO在你的应用程序中是如何发挥作用的。假设Person
对象和Account
对象交互,表示一个人在银行的储蓄账户。Person
的实例可能需要知道Account
实例某些方面何时发生变化,比如余额或者利率。
If these attributes are public properties of
Account
, thePerson
could periodically poll theAccount
to discover changes, but this is of course inefficient, and often impractical. A better approach is to use KVO, which is akin toPerson
receiving an interrupt when a change occurs.
如果这些属性时Account
的公开属性,Person
可以定期轮询Account
来发现变化,但是这样是低效的,而且通常不切实际。一个更好的方法时使用KVO,类似于当变化发生时Person
接收中断。
To use KVO, first you must ensure that the observed object, the
Account
in this case, is KVO compliant. Typically, if your objects inherit fromNSObject
and you create properties in the usual way, your objects and their properties will automatically be KVO Compliant. It is also possible to implement compliance manually. KVO Compliance describes the difference between automatic and manual key-value observing, and how to implement both.
要使用KVO,首先必须确定被观察的对象(这个例子中的Account
)符合KVO。通常,如果你的对象继承自NSObject
而且使用常规方式创建属性,你的对象和他们的属性将自动遵从KVO,也可以手动实现。 KVO Compliance介绍了自动和手动KVO的不同以及如何实现它们。
Next, you must register your observer instance, the
Person
, with the observed instance, theAccount
.Person
sends anaddObserver:forKeyPath:options:context:
message to theAccount
, once for each observed key path, naming itself as the observer.
接下来,必须通过被观察的实例(Account
)注册观察者实例(Person
)。Person
给Account
发送addObserver:forKeyPath:options:context:
消息,每个被观察的键发送一次,将它自己命名为观察者。
In order to receive change notifications from the
Account
,Person
implements theobserveValueForKeyPath:ofObject:change:context:
method, required of all observers. TheAccount
will send this message to thePerson
any time one of the registered key paths changes. ThePerson
can then take appropriate action based upon the change notification.
为了从Account
接受变化的通知,Person
实现了所有观察者必需实现的方法observeValueForKeyPath:ofObject:change:context:
。只要其中一个注册的键路径发生变化,Account
就会把这个消息发送给Person
。然后,Person
可以根据变化通知采取适当的操作。
Finally, when it no longer wants notifications, and at the very least before it is deallocated, the
Person
instance must de-register by sending the messageremoveObserver:forKeyPath:
to theAccount
.
最后,当不想要接收通知时,并且至少在它被销毁之前,Person
实例必须通过向Account
发送消息removeObserver:forKeyPath:
来取消注册。
个人总结:
1.KVO是一种观察者机制,允许一个对象观察其他对象的属性的变化。
2.使用KVO整体分为四步:
- 确定被观察者对象允许使用KVO(对象继承自NSObject并且通过setter或KVC对属性赋值)
- 注册观察者对象
- 观察者实现回调方法
- 观察者移除观察