Thanks Ken,

I was able to solve the problem by not using NSMutableArray's 
addObjectsFromArray method.  Instead I replaced it with:

NSMutableArray * eventsArrayProxy = [self mutableArrayValueForKey:@"events"];   
        NSRange range = NSMakeRange([events count], [appointments count]);
        NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:range];
        
[eventsArrayProxy insertObjects:appointments atIndexes:indexes];

And now the updates are sent to the observer all at once.

My own personal conclusion is that insertObjects:atIndexes: works much better 
with KVO then addObjectsFromArray:

Thanks!
Mazen Abdel-Rahman


On Jun 9, 2010, at 3:13 PM, Ken Thomases wrote:

> On Jun 9, 2010, at 3:19 PM, 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];
>> 
>> Which naturally causes the following method in my view class to be invoked:
>> 
>> -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
>> change:(NSDictionary *)change context:(void *)context
>> 
>> My problem is that observeValueForKeyPath.. is invoked once for each and 
>> every single item in the appointments array (the array being added) - rather 
>> than once for all of them.  Is this standard behavior?  And is there a way 
>> to have it call observeValueForKeyPath.. one time for all the objects being 
>> added?
>> 
>> In my case - I am sometimes adding as much as a thousand or more objects - 
>> so it is a severe performance issue right now.  If this is the only way KVO 
>> works for arrays then I will have to find another way around it - but I'm 
>> hopeful perhaps there's some way this can be done.
> 
> You don't say how the "events" property is implemented in terms of accessor 
> methods.
> 
> Be sure to read these:
> 
> <http://developer.apple.com/mac/library/documentation/cocoa/conceptual/KeyValueCoding/Concepts/AccessorConventions.html#//apple_ref/doc/uid/20002174-178830-BAJEDEFB>
> <http://developer.apple.com/mac/library/documentation/cocoa/conceptual/KeyValueCoding/Concepts/SearchImplementation.html#//apple_ref/doc/uid/20000955-SW2>
> <http://developer.apple.com/mac/library/documentation/cocoa/conceptual/KeyValueCoding/Concepts/Performance.html#//apple_ref/doc/uid/20002175-171187>
> 
> Implementing the multiple-object variants of the mutating indexed accessor 
> methods will almost certainly alleviate the problem.  By the way, if you 
> implement those indexed accessor methods, then you don't need to use 
> -mutableArrayValueForKey:.  You can just directly message "self" with the 
> appropriate accessor.
> 
> Regards,
> Ken
> 

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to