Re: Manual KVO and will/didChangeValueForKey
Re: Manual KVO and will/didChangeValueForKey
- Subject: Re: Manual KVO and will/didChangeValueForKey
- From: Andrew White <email@hidden>
- Date: Fri, 03 Jun 2005 10:42:45 +1000
mmalcolm crawford wrote:
On Jun 1, 2005, at 11:51 PM, Tim Lucas wrote:
I think willChangeValueForKey: just gives the any NSEditor's the
chance to finish their editing (anybody confirm this?) -- it won't
send out a KVO change notification.
Umm, no. It gives the observation mechanism an opportunity to record
pre-change values.
Have you tried calling [self (will/did) ChangeValueForKey:@"property"]
from inside your observeValueForKeyPath: method?
It's not clear what purpose this would serve?
If willChangeValueForKey allows the KVO system a chance to record
pre-change values then I have a potential problem.
Here's the situation:
@implementation A
- (id) property
{
return <somevalue>;
}
- (void) makeAChange
{
[self willChangeValueForKey: @"property"];
<change property>
[self didChangeValueForKey: @"property"];
}
@end
The above is all OK. The problem comes here...
@implementation B
- (id) anotherProperty
{
return [_a property];
}
@end
If I want to KVO on B.anotherProperty then I need to correctly send
willChangeValueForKey and didChangeValueForKey based on when A.property
changes. The naive implementation is
@implementation B (KVO)
- (void) observeValueForKeyPath: (NSString *) keyPath
ofObject: (id) object
change: (NSDictionary *) change
context: (void *) context
{
if (object == _a)
{
if ([keyPath isEqualToString: @"property"])
{
[self willChangeValueForKey: @"anotherProperty"];
[self didChangeValueForKey: @"anotherProperty];
return;
}
<...>
}
<...>
}
@end
Except that in the above code, willChangeValueForKey is obviously lying,
since the value of the key has *already* changed. In order to call
willChangeValueForKey truthfully in the dependent key, I need to trap the
willChangeValueForKey call in the source key. If I only get notified on
didChangeValueForKey then it is too late.
Why not just make a mirror? The key in question is a wrapper around a
somewhat complex data structure. I'm not sure that the
"willChangeValueForKey" semantics of recording the 'old' copy even make
sense. A shallow mirror will only mirror the wrapper, not the detail. A
deep mirror would be an unecessarily expensive operation.
Any thoughts?
--
Andrew White
--------------------------------------------------------------------------
This email and any attachments may be confidential. They may contain legally
privileged information or copyright material. You should not read, copy,
use or disclose them without authorisation. If you are not an intended
recipient, please contact us at once by return email and then delete both
messages. We do not accept liability in connection with computer virus,
data corruption, delay, interruption, unauthorised access or unauthorised
amendment. This notice should not be removed.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden