Re: Key Value Observing?
Re: Key Value Observing?
- Subject: Re: Key Value Observing?
- From: mmalc crawford <email@hidden>
- Date: Wed, 12 Dec 2007 11:11:52 -0800
On Dec 11, 2007, at 10:31 PM, Steve Steinitz wrote:
The article says 'You must add and remove the parent as an observer
as child objects are added to and removed from the relationship'.
Inconvenient but fair enough. However, that implies that when a
parent is instantiated it needs to loop through all its existing
children and register to observe their innards. If that is the
case, wouldn't it give a performance hit for any application, like
mine, that load tens of thousands of parent objects at startup.
Would that be correct?
For the first implementation, it may be... If measurement determines
that this is a significant overhead, then it may be more appropriate
to adopt the second approach:
"You can register the parent with the application's notification
center as an observer of its managed object context. The parent should
respond to relevant change notifications posted by the children in a
manner similar to that for key-value observing."
Let me share an evil hack I've done while I await the answer to
those questions. I didn't set Sale up as an observer for the
LineItems at all -- reluctant to code and debug it and debug the
registering / unregistering in the absence of answers. Instead, I
just overrode
LineItem setQuantity and
LineItem setSalePrice
to call my brand new dummy
Sale setTotal
method which does nothing more than call didChange (Sale has no
total ivar/attribute and the calculation is done in Sale total).
Bingo. Maintenance and encapsulation aside, I'm out of hot water
with my client.
I wouldn't recommend doing that...
At the very least you should always invoke the willChange method
first. Second, you shouldn't invoke the change notification methods
after the change has already been made just as a way of "tickling" an
update.
You *might* get away with:
- (void)setSalePrice:(NSNumber *)newPrice
{
if (![[self salePrice] isEqualToNumber:newPrice])
{
[[self sale] willChangeValueForKey:@"total"];
[self willChangeValueForKey:@"salePrice"];
[self setPrimitiveValue:newPrice forKey:@"salePrice"];
[self didChangeValueForKey:@"salePrice"];
[[self sale] didChangeValueForKey:@"total"];
}
}
(typed in Mail).
I need to check, though...
mmalc
_______________________________________________
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