views:

55

answers:

1

I have a fairly simple iPhone app that downloads a set of UITableView results into its model class, and a view controller that's set up to observe these changes using KVO.

This system works well (much better than scattering update code everywhere), except that when I get results, I add them to the backing NSMutableArray one-by-one. This fires many KVO notifications, staggering my row animations in a way that looks odd. Is there a way to coalesce certain KVO notifications so the changes can all happen at once and thus provide a single KVO notification with a single index set?

Alternatively, if I try calling -addObjectsFromArray to add my new results in a batch fashion, the necessary KVO notifications are never sent, so that must not be one of the observed methods, right? Would it be better to take care of this functionality myself by wrapping my changes with will/didChangeValueForKey calls and generating the appropriate index set?

A: 

Hi,

I'm having exactly the same problem. I dont have a proper solution yet but you may find some answers on this page of the KVO programming guide :

You can implements these methods to add severals objects at once to your array :

-insert<Key>:atIndexes: and -remove<Key>AtIndexes
(corresponding to the NSMutableArrayinsertObjects:atIndexes: and removeObjectsAtIndexes: methods)

Hope this helps, Vincent.

vdaubry
Hah, that's not what I asked for, but it was totally what I needed! Thanks!When I receive a new data set, I just add it to my backing NSMutableArray using that method (in this example, `videos` is an NSArray with the new data):`[self insertItems:videos atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [videos count])]];`
Collin Allen