On Jun 9, 2010, at 13:19, Mazen M. Abdel-Rahman wrote:

> I have a view class that observes changes in a model class's array via KVO.  
> Unfortunately - when ever the array is updated - the updates are sent to the 
> view one at at a time.
> 
> In my model class I have the following line:
> 
>       NSMutableArray * eventsArrayProxy = [self 
> mutableArrayValueForKey:@"events"];
>       [eventsArrayProxy addObjectsFromArray:appointments];

You could guarantee that there's only a single KVO notification in a case like 
this, by doing it the "old-fashioned" way:

        NSMutableArray* newEvents = [[self valueForKey: @"events"] 
mutableCopy]; // or something equivalent
        [newEvents addObjectsFromArray: appointments];
        [self setValue: newEvents forKey @"events"]; // or something equivalent

That would produce only a single notification, though of course the 
notification wouldn't tell you which array elements had changed.

Getting back to the original problem, I suspect the reason that it's producing 
multiple notification arises from your implementation. If you look in 
NSKeyValueCoding.h, in the comments immediately preceding the 
'mutableArrayValueForKey:' declaration, you'll see that case 2 is flagged as a 
potential performance problem.

The solution is therefore to implement indexed accessors (case 1) in your 
class. (Or, remove your setter and let KVC access your array variable directly 
-- case 3 -- but generally direct instance variable access is something to be 
avoided.)


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to