CoreData undo and observing related objects
CoreData undo and observing related objects
- Subject: CoreData undo and observing related objects
- From: Martin Wennerberg <email@hidden>
- Date: Fri, 17 Nov 2006 13:11:53 +0100
I'm using observing to update attributes based on changes in related
objects.
I do this by implementing accessor methods:
// objA watches objB for keyPath
@implementation ObjA
- (void) setObjB:(ObjB *)newValue
{
id oldValue = [self objB];
if (oldValue)
[oldValue removeObserver:self keyPath:keyPath];
[self willChangeValueForKey:@"objB"];
[self setPrimitiveValue:newValue forKey:@"objB"];
[self didChangeValueForKey:@"objB"];
if (newValue)
[newValue addObserver:self forKeyPath:keyPath options:0 context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
// Do something
}
@end
This works fine when I change the value through code or GUI, but when
NSUndoManager does it's work the accessor methods aren't called do my
observing isn't updated.
So my next attempt was to have objA observe it's own property objB,
but that does not seem to be trigged by undo either:
@implementation ObjB
- (void) startObservingSelf
{
[newValue addObserver:self forKeyPath:@"objB" options:
(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];
}
- (void) awakeFromFetch
{
[self startObservingSelf];
}
- (void) awakeFromInsert
{
[self startObservingSelf];
}
- (void)observeValueForKeyPath:(NSString *)key ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (object == self)
{
// stop observing the old value
// start observing the new value
}
else if ([key isEqualToString:keyPath])
// do something
...
}
@end
I would have expected that undo would trigger the observation, but it
seems it does not. Should it?
How is this usually done?
Martin Wennerberg
Norrkross Software
_______________________________________________
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