Key Value Observing

Key Value Observing (KVO) is the sister API to KVC. KVO allows us to request notifications when an attribute has changed. By observing attributes on an object, we can react when those attributes are changed. KVO is also implemented via an informal protocol on the NSObject, and we register and remove observers using ‑addObserver:for-KeyPath:options:context: and ‑removeObserver:forKeyPath:. These are the two primary methods, although there are other methods involved in the protocol, just as with KVC.

If we wanted to observe the name value on a recipe, we’d add ourselves (or another object) as an observer for that value, like so:

 static​ NSString *kPragProgObserver = ​@"PragProgObserver"
 id myRecipe = ...
 [myRecipe addObserver:self ...

Get Core Data in Objective-C, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.