I have an NSManagedObject
with two properties:
NSNumber *score; NSDate *score_timestamp;
I need to update the score_timestamp
field every time I update the score
.
I obviously cannot use the -willSave
method, since my context is sometimes saved and score_timestamp
will not be updated. Therefore, I must either override -setScore:
or configure my managed entity as an observer with a key for my own score
field.
-setScore:
solution -setScore:
seems simple:
- (void) setScore:(NSNumber *)score { [self willChangeValueForKey:@"score"]; [self setPrimitiveScore:score]; [self didChangeValueForKey:@"score"]; self.score_timestamp = [NSDate date]; }
Are there any warnings on this? Or should I use a KVO solution?
Update
So far I have received two answers that my code will not work through setValue: forKey:
and I'm still waiting, for example. A naive call to [(NSManagedObject *)myObject setValue:value forKey:@"score"]
still calls my setter.
So, if I switch to the KVO solution, should I addObserver:
in all awake
methods and delete it in willTurnIntoFault
? Or is it not that simple?
ihunter
source share