diff --git a/README.md b/README.md index 570f441c2..ee782cf02 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Source/Categories/NSManagedObject+MagicalRecord.m b/Source/Categories/NSManagedObject+MagicalRecord.m index 70d974d46..c0946f418 100644 --- a/Source/Categories/NSManagedObject+MagicalRecord.m +++ b/Source/Categories/NSManagedObject+MagicalRecord.m @@ -737,6 +737,38 @@ - (id) MR_objectWithMinValueFor:(NSString *)property 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 {