• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
CoreData undo and observing related objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: CoreData undo and observing related objects
      • From: Martin Wennerberg <email@hidden>
  • Prev by Date: [SOLVED] QTKit : Creating and saving a movie Reference to an URL
  • Next by Date: IOKit notifications
  • Previous by thread: [SOLVED] QTKit : Creating and saving a movie Reference to an URL
  • Next by thread: Re: CoreData undo and observing related objects
  • Index(es):
    • Date
    • Thread