Skip to content

Commit

Permalink
Adding aggregate operation support submitted from Duane Fields. Thank…
Browse files Browse the repository at this point in the history
…s for submitting!
  • Loading branch information
casademora committed Nov 15, 2011
1 parent 3e0b36c commit 26d74ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,9 @@ Or, to have the results sorted by a property:


NSArray *peopleSorted = [Person findAllSortedByProperty:@"LastName" ascending:YES]; 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: If you have a unique way of retrieving a single object from your data store, you can get that object directly:


Expand Down Expand Up @@ -129,6 +132,12 @@ There are also counterpart methods which return NSUInteger rather than NSNumbers
* countOfEntitiesWithPredicate:(NSPredicate *) * countOfEntitiesWithPredicate:(NSPredicate *)
* countOfEntitiesWithPredicate:(NSPredicate *) inContext:(NSManagedObjectContext *) * 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 #### Finding from a different context


All find, fetch and request methods have an inContext: method parameter All find, fetch and request methods have an inContext: method parameter
Expand Down
32 changes: 32 additions & 0 deletions Source/Categories/NSManagedObject+MagicalRecord.m
Expand Up @@ -737,6 +737,38 @@ - (id) MR_objectWithMinValueFor:(NSString *)property
return [self MR_objectWithMinValueFor:property inContext:[self managedObjectContext]]; 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 - (id) MR_inContext:(NSManagedObjectContext *)otherContext
{ {
Expand Down

0 comments on commit 26d74ec

Please sign in to comment.