开发者

Querying max, min and other via Core Data

Is it possible to que开发者_如何学编程ry the database for the max, min and other values using Core Data framework?


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


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 .


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜