mongeta
mongeta

Reputation: 2301

Aggregate and sum using Core Data

I have two entities:

I want to get the sum of invoices grouped by customers.

After reading some web pages, tutorials, and Apple documentation, it is not clear to me what will be the better approach, using Core Data.

The current solution that works for me, is fetching all customers, and for each one, fetch again only the sum of invoices. Even it is working perfectly, I do not like as a definitive solution.

Maybe using the NSExpressionDescription would be a better approach, but really I do not understand it well enough to use it.

I know Core Data is not a database, and in plain SQL this is really easy, but I am using Core Data :-)

Any advice will be appreciate :-)

thanks,

ps. I have the Marcus Zarra Core Data book but can not find a solution/guidance there

Upvotes: 0

Views: 1614

Answers (1)

MrMage
MrMage

Reputation: 7487

As long as your dataset isn't too large, I'd suggest just using something like [customer valueForKeyPath:@"@sum.invoices.amount"]. If you'd like to try to have the sum calculated by the database (Core Data uses SQLite as its backend most of the time), have a look at http://iphonedevelopment.blogspot.com/2010/11/nsexpression.html.

Upvotes: 1

Related Questions