mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 17:32:18 +08:00
Adding aggregate operation support submitted from Duane Fields. Thanks for submitting!
This commit is contained in:
@@ -79,6 +79,9 @@ Or, to have the results sorted by a property:
|
||||
|
||||
NSArray *peopleSorted = [Person findAllSortedByProperty:@"LastName" ascending:YES];
|
||||
|
||||
Or, to have the results sorted by multiple properties:
|
||||
|
||||
NSArray *peopleSorted = [Person findAllSortedByProperty:@"LastName,FirstName" ascending:YES];
|
||||
|
||||
If you have a unique way of retrieving a single object from your data store, you can get that object directly:
|
||||
|
||||
@@ -129,6 +132,12 @@ There are also counterpart methods which return NSUInteger rather than NSNumbers
|
||||
* countOfEntitiesWithPredicate:(NSPredicate *)
|
||||
* countOfEntitiesWithPredicate:(NSPredicate *) inContext:(NSManagedObjectContext *)
|
||||
|
||||
#### Aggregate Operations
|
||||
|
||||
NSPredicate *prediate = [NSPredicate predicateWithFormat:@"diaryEntry.date == %@", today];
|
||||
int totalFat = [[CTFoodDiaryEntry aggregateOperation:@"sum:" onAttribute:@"fatColories" withPredicate:predicate] intValue];
|
||||
int fattest = [[CTFoodDiaryEntry aggregateOperation:@"max:" onAttribute:@"fatColories" withPredicate:predicate] intValue];
|
||||
|
||||
#### Finding from a different context
|
||||
|
||||
All find, fetch and request methods have an inContext: method parameter
|
||||
|
||||
@@ -737,6 +737,38 @@ static NSUInteger defaultBatchSize = kMagicalRecordDefaultBatchSize;
|
||||
return [self MR_objectWithMinValueFor:property inContext:[self managedObjectContext]];
|
||||
}
|
||||
|
||||
+ (NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context
|
||||
{
|
||||
NSExpression *ex = [NSExpression expressionForFunction:function
|
||||
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:attributeName]]];
|
||||
|
||||
NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
|
||||
[ed setName:@"result"];
|
||||
[ed setExpression:ex];
|
||||
|
||||
// determine the type of attribute, required to set the expression return type
|
||||
NSAttributeDescription *attributeDescription = [[[self entityDescription] attributesByName] objectForKey:attributeName];
|
||||
[ed setExpressionResultType:[attributeDescription attributeType]];
|
||||
NSArray *properties = [NSArray arrayWithObject:ed];
|
||||
MR_RELEASE(ed);
|
||||
|
||||
NSFetchRequest *request = [self requestAllWithPredicate:predicate inContext:context];
|
||||
[request setPropertiesToFetch:properties];
|
||||
[request setResultType:NSDictionaryResultType];
|
||||
|
||||
NSDictionary *resultsDictionary = [self executeFetchRequestAndReturnFirstObject:request];
|
||||
NSNumber *resultValue = [resultsDictionary objectForKey:@"result"];
|
||||
|
||||
return resultValue;
|
||||
}
|
||||
|
||||
+ (NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate
|
||||
{
|
||||
return [self aggregateOperation:function
|
||||
onAttribute:attributeName
|
||||
withPredicate:predicate
|
||||
inContext:[NSManagedObjectContext defaultContext]];
|
||||
}
|
||||
|
||||
- (id) MR_inContext:(NSManagedObjectContext *)otherContext
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user