I have a entitiy in my NSMAnagedObject that is depended on other enititys, so after reading the documention on Depend Keys I come up with the following within my subclass
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"assetAmount"]) {
NSArray *affectingKeys = @[@"assetAlternativeCur", @"assetAltCur", @"assetCurrency"];
keyPaths = [keyPaths setByAddingObjectsFromArray:affectingKeys];
}
return keyPaths;
}
- (void)setAssetAmount:(NSDecimalNumber *)assetAmount
{
[self willChangeValueForKey:@"assetAmount"];
if ([[self useAlternativeCur] boolValue] == YES) {
NSDecimalNumber *result;
result = [[self assetConversionRate] decimalNumberByMultiplyingBy:[self assetAlternativeCur]];
[self setPrimitiveAssetAmount:result];
} else {
[self setPrimitiveAssetAmount:assetAmount];
}
[self didChangeValueForKey:@"assetAmount"];
}
My problem is the setter "setAssetAmount" only gets called when I change the "assetAmount" value directly, if a change the values included in the keyPathsForValuesAffectingValueForKey the setter does not get called. Am I going about this the wrong way? I expected the setter to get called each time any value changes.