Custom setter methods in Core-Data
I think there is a slight mistake: use
[self setPrimitiveValue:inFoo forKey:@"foo"];
instead of
[self setPrimitiveFoo:inFoo];
this works for me.
According to the documentation, it'd be:
- (void) setFoo:(NSObject *)inFoo {
[self willChangeValueForKey:@"foo"];
[self setPrimitiveValue:inFoo forKey:@"foo"];
[self didChangeValueForKey:@"foo"];
}
This is, of course, ignoring the fact that NSManagedObjects
only want NSNumbers
, NSDates
, NSDatas
, and NSStrings
as attributes.
However, this might not be the best approach. Since you want something to happen when the value of your foo
property changes, why not just observe it with Key Value Observing? In this case, it sounds like "KVO's the way to go".
Here's how I'm doing KVO on the id
attribute of a Photo : NSManagedObject
. If the photo's ID changes, then download the new photo.
#pragma mark NSManagedObject
- (void)awakeFromInsert {
[self observePhotoId];
}
- (void)awakeFromFetch {
[self observePhotoId];
}
- (void)observePhotoId {
[self addObserver:self forKeyPath:@"id"
options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"id"]) {
NSString *oldValue = [change objectForKey:NSKeyValueChangeOldKey];
NSString *newValue = [change objectForKey:NSKeyValueChangeNewKey];
if (![newValue isEqualToString:oldValue]) {
[self handleIdChange];
}
}
}
- (void)willTurnIntoFault {
[self removeObserver:self forKeyPath:@"id"];
}
#pragma mark Photo
- (void)handleIdChange {
// Implemented by subclasses, but defined here to hide warnings.
// [self download]; // example implementation
}