background preloader

KVO

Facebook Twitter

iPhone Dev Center: KVO Quick Start: Key-Value Observing Quick St. KVO. iPhone Dev Center: NSKeyValueObserving Protocol Reference. (informal protocol) Overview The NSKeyValueObserving (KVO) informal protocol defines a mechanism that allows objects to be notified of changes to the specified properties of other objects.

iPhone Dev Center: NSKeyValueObserving Protocol Reference

You can observe any object properties including simple attributes, to-one relationships, and to-many relationships. Using KVO for Table Updates. If you've followed the guidelines in Apple's Model Object Implementation Guide when creating your data model objects, you can handle your UITableView updates using KVO.

Using KVO for Table Updates

This frees you from having to spread calls to reloadData, insertRowsAtIndexPaths:withRowAnimation:, or deleteRowsAtIndexPaths:withRowAnimation: throughout your controller class wherever the data in your table might get changed. Instead, all you have to do is observe the keypath on your data model object that holds the collection of items being displayed in the table. The easiest way to do that is in your table view controller's viewDidLoad method, like so: Then, you just implement observeValueForKeyPath:ofObject:change:context to insert or remove rows based on changes to the observed collection. The information about which rows were inserted or deleted comes in the changes dictionary stored under the key NSKeyValueChangeIndexesKey.