I have a simple Core Data store with an entity Cost with an integer property “value”. I want to sum up all amounts of the costs in my store, which is equivalent to the following sql statement:
SELECT sum(value) FROM costs
How do I do it the most efficient way in Cocoa Touch? By using Core Data? Or just get all cost entities and do the summation manually?
The best way is to use a fetch for specific values and supply a NSExpressionDescription with a
sum:function.When you execute the fetch you get a one element array containing a dictionary whose keys match the expression descriptions and whose values are the results of the expressions. In this case, you would get a
sumkey whose value would be the sum of the attributes given the expression.