The master data gets the sum of the values. Selected Properties Against Distribution

I'm relatively new to Core Data (coming from SQLite background). I just finished reading the book "Core Data for iOS", but I still had a number of perplexing questions when I started modeling an application that has the following model:

  • The Accounts object, which is related to many "transactions" and the "startBalance" property
  • The Transaction object, which has a “many payments” relationship (and vice versa for accounts).
  • Object "Payment", which contains data on the actual "amount" paid

For performance reasons, I wanted to de-normalize the model and add the “TotalAmountSpent” property to the “Accounts” entity (as suggested in the book) so that I could just update it when something changed.

In practice, this is difficult to accomplish with Core Data. I can’t figure out how to do it right (and I don’t know how to do it right). So my questions are:

a) Should instead change "TotalAmountSpent" to Fetched Property? Are there any performance implications (I know that it is loaded lazily, but I will almost certainly retrieve this property for each account). If I do this, I will need to get the total amount spent against "startBalance" for a certain period of time (for example, for the last three days). It seems easy in SQL, but how to do it in Core Data? I read that I can use the @sum aggregation function, but how can I filter the “date” using @sum? I also read that any change in the data will require updating the selected property. How do I “listen” to a change? Am I doing this in the 'PaymentSMS' method of the 'Payment' object?

b) "TotalAmountSpent" , ? ? "willSave" NSManagedObject? , , /, "startBalance" . . ,

. !

+5
1

, . - .

.

  • -awakeFromFetch/-awakeFromInsert , . , KVO (Key Value Observer) , . KVC KVO - .

  • -willSave NSManagedObject . , , , , .

, KVC Collection Operators. :

NSNumber *sum = [self valueForKeyPath:@"transactions.@sum.startingBalance"];
+7