Re: Adding objects from one array to another and and Key Value Observing
Re: Adding objects from one array to another and and Key Value Observing
- Subject: Re: Adding objects from one array to another and and Key Value Observing
- From: Ken Thomases <email@hidden>
- Date: Wed, 9 Jun 2010 17:13:25 -0500
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 (email@hidden)
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:
This email sent to email@hidden