Chiron
Chiron

Reputation: 20245

Querying max, min and other via Core Data

Is it possible to query the database for the max, min and other values using Core Data framework?

Upvotes: 2

Views: 5291

Answers (3)

Naftic
Naftic

Reputation: 111

You can use query builder, which allows you write your requests like this:

NSDictionary *productTotalSumAndAveragePriceGroupedByCountries = 
[[[[[Product all
] aggregatedBy:@[
                 @[kAggregateSum, @"amount"],
                 @[kAggregatorAverage, @"price"]]
] groupedBy:@[@"country"]
] having:predicate
] execute];

Upvotes: 0

lomaxx
lomaxx

Reputation: 115763

You need to use the collection operators in objective c. More info here: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html

Upvotes: 8

Nick Hutchinson
Nick Hutchinson

Reputation: 5189

You can indeed have an NSFetchRequest calculate such values. You need to set your fetch request to return NSDictionaryResultType results, and create appropriate NSExpressionDescription objects to describe the operations (e.g. min, max, sum, etc.) you're after.

Take a look at the worked example in Apple's Core Data Programming Guide: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484-SW6 .

Upvotes: 0

Related Questions