Re: CoreData - re-faulting object and removing self-observers results in messages being sent to zombie
Re: CoreData - re-faulting object and removing self-observers results in messages being sent to zombie
- Subject: Re: CoreData - re-faulting object and removing self-observers results in messages being sent to zombie
- From: Jim Correia <email@hidden>
- Date: Wed, 3 Aug 2005 10:16:51 -0400
On Aug 2, 2005, at 8:30 PM, Bill Bumgarner wrote:
Instead, why not make the lastModifiedTime a dependent key on the
rest of the object's keys?
In particular:
- make lastModifiedTime dependent upon all keys that need to update
the lastModifiedTime
- override willChangeValueForKey: to:
- call super first
- check to see if the key is lastModifiedTime
- if it is, then use primitive KVC to set the lastModifiedTime
to now
The above should work.
Unless I've misunderstood what you are suggesting, it doesn't seem to
work. I modified Person from yesterday's example to do this:
@implementation Person
+ (void)initialize
{
[Person setKeys: [NSArray arrayWithObjects: @"firstName",
@"lastName", nil] triggerChangeNotificationsForDependentKey:
@"lastEdited"];
}
- (void)awakeFromInsert
{
[super awakeFromInsert];
NSManagedObject *resume = [NSEntityDescription
insertNewObjectForEntityForName: @"Resume" inManagedObjectContext:
[self managedObjectContext]];
[self setValue: resume forKey: @"resume"];
// [self addObserver: self forKeyPath: @"lastEdited" options: 0
context: NULL];
}
- (void)willChangeValueForKey:(NSString *)key
{
[super willChangeValueForKey: key];
NSLog(@"key = %@", key);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"keyPath changed = %@", keyPath);
}
@end
-willChangeValueForKey doesn't fire for lastModified unless I add
myself as an observer for lastModified (which is what got me into hot
water to begin with.)
Alternatively, you could punt on the dependent key stuff and just
make willChangeValueForKey: always update the lastModifiedTime,
regardless of key (via the primitive KVC as described above).
The snippet from last night seemed to work - except for changing the
lastModified date when a to-one relationship object -
blob.attributedString for example - changes.
If there isn't a good reason not to do it this way, I can use this.
If there is some particularly elegant way of dealing with the
blob.attributedString problem I'm all ears - if not there is at least
one inelegant solution.
Here's the code again for reference:
- (void)didChangeValueForKey:(NSString *)key
{
[super didChangeValueForKey: key];
if ([modificationDateDependentProperties containsObject: key])
{
NSUndoManager *undoManager = [[self managedObjectContext]
undoManager];
if (! ([undoManager isUndoing] || [undoManager isRedoing]))
[self touch];
}
}
Thanks,
Jim
_______________________________________________
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