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: Quincey Morris <email@hidden>
- Date: Wed, 9 Jun 2010 15:06:45 -0700
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@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