views:

151

answers:

1

I have an attribute in a Core Data Managed Object that I'm trying to update dependent on another attribute.

How can I implement a method that is called every time the original attribute is changed?

awakeFromInsert and awakeFromFetch obviously won't work. I've seen keyPathsForValuesAffectingValueForKey but I don't really understand how I could use it.

Thanks a bunch

+2  A: 

You're on the right track. Let's say you've got two attributes: foo, and bar, with bar being calculated based on foo. For this, you need to implement the +keyPathsForValuesAffectingBar method:

+ (NSSet *)keyPathsForValuesAffectingBar {
    return [NSSet setWithObject:@"foo"];
}

Now, every time foo changes, any objects bound to bar will update themselves.

Obviously, this wont do much good if you're caching the value of bar, since you need to actually recalculate and recache it. However, unless you're doing some seriously hardcore calculations to determine bar, you're really better off just calculating bar every time the -bar method is called.

Matt Ball