开发者

Querying max, min and other via Core Data

开发者 https://www.devze.com 2023-02-06 18:52 出处:网络
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://develo

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

精彩评论

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