I\'m relatively new to Core Data (coming from an SQLite background). Just finished reading the \'Core Data for iOS\' book but I\'m left with a number of baffling questions w
If you use a fetched property you cannot then query on that property easily without loading the data into memory first. Therefore I recommend you keep the actual de-normalized data in the entity instead.
There are actually a few ways to easily keep this up to date.
In your -awakeFromFetch
/-awakeFromInsert
set up an observer of the relationship that will impact the value. Then when the KVO (Key Value Observer) fires you can do the calculation and update the field. Learning KVC and KVO is a valuable skill.
You can override -willSave
in the NSManagedObject
subclass and do the calculation on the save. While this is easier, I do not recommend it since it only fires on a save and there is no guarantee that your account object will be saved.
In either case you can do the calculation very quickly using the KVC Collection Operators. With the collection operators you can do the sum via a call to:
NSNumber *sum = [self valueForKeyPath:@"transactions.@sum.startingBalance"];