Skip to content
This repository

added grouping version of aggregate methods #113

Closed
wants to merge 9 commits into from

4 participants

Duane Fields Brian King Saul Mora Tony Arnold
Duane Fields

These methods allow you group aggregate results around a property, rather than the whole table. Results are an array of dictionaries.

Tony Arnold
Owner

You should really just push this onto the clang diagnostic stack when you need it:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

// Your code that throws a warning you cannot possibly work around

#pragma clang diagnostic pop

Don't just disable it for the whole category, that'll mask other problems down the track.

Thanks, wasn't aware you could do that... I'll update it, thanks

Brian King

Any reason why this wasn't accepted? Looks like just what I need. If you want someone to update the branch and re-push I'll gladly help!

Saul Mora
Owner

I'm pretty sure these methods were added to MR a while ago. I don't remember if automatic merging was available back then. Also, I may have inadvertently messed up the merge to give you proper attribution :(

Brian King

Must of gotten dropped by a merge, there's no groupBy: in +MagicalAggregation.h.

And to be clear on attribution, I didn't write it, I just need it! ;)

Also, there is a multi-context bug in this groupBy function, it doesn't pass the context with MR_executeFetchRequest:

Tony Arnold
Owner

Closed in lieu of #457, which is the same code with the correct context being passed to the fetch request. It still needs tests if anyone can come up with a few good ideas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  README.md
Source Rendered
@@ -154,6 +154,7 @@ There are also counterpart methods which return NSUInteger rather than NSNumbers
154 154 NSPredicate *prediate = [NSPredicate predicateWithFormat:@"diaryEntry.date == %@", today];
155 155 int totalFat = [[CTFoodDiaryEntry aggregateOperation:@"sum:" onAttribute:@"fatColories" withPredicate:predicate] intValue];
156 156 int fattest = [[CTFoodDiaryEntry aggregateOperation:@"max:" onAttribute:@"fatColories" withPredicate:predicate] intValue];
  157 + NSArray* caloriesByMonth = [CTFoodDiaryEntry aggregateOperation:@"sum:" onAttribute:@"fatColories" withPredicate:predicate groupBy:@"month"];
157 158
158 159 #### Finding from a different context
159 160
3  Source/Categories/NSManagedObject+MagicalDataImport.m
@@ -164,6 +164,8 @@ - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary
164 164 SEL shouldImportSelector = @selector(shouldImport:);
165 165 BOOL implementsShouldImport = [self respondsToSelector:shouldImportSelector];
166 166
  167 +#pragma clang diagnostic push
  168 +#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
167 169 if (![self MR_importValue:relatedObjectData forKey:relationshipName])
168 170 {
169 171 if ([relationshipInfo isToMany])
@@ -185,6 +187,7 @@ - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary
185 187 }
186 188 }
187 189 }
  190 +#pragma clang diagnostic pop
188 191 }
189 192 }
190 193
4 Source/Categories/NSManagedObject+MagicalRecord.h
@@ -99,6 +99,10 @@
99 99 + (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending;
100 100 + (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
101 101
  102 +#ifdef __IPHONE_5_0
  103 ++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath inContext:(NSManagedObjectContext *)context;
  104 ++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath;
  105 +#endif
102 106 + (NSNumber *)MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context;
103 107 + (NSNumber *)MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate;
104 108
35 Source/Categories/NSManagedObject+MagicalRecord.m
@@ -732,6 +732,41 @@ - (id) MR_objectWithMinValueFor:(NSString *)property
732 732 return [self MR_objectWithMinValueFor:property inContext:[self managedObjectContext]];
733 733 }
734 734
  735 +#ifdef __IPHONE_5_0
  736 +
  737 ++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath inContext:(NSManagedObjectContext *)context
  738 +{
  739 + NSExpression *ex = [NSExpression expressionForFunction:function
  740 + arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:attributeName]]];
  741 +
  742 + NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
  743 + [ed setName:@"result"];
  744 + [ed setExpression:ex];
  745 +
  746 + // determine the type of attribute, required to set the expression return type
  747 + NSAttributeDescription *attributeDescription = [[[self MR_entityDescription] attributesByName] objectForKey:attributeName];
  748 + [ed setExpressionResultType:[attributeDescription attributeType]];
  749 + NSArray *properties = [NSArray arrayWithObjects:groupingKeyPath, ed, nil];
  750 + MR_RELEASE(ed);
  751 +
  752 + NSFetchRequest *request = [self MR_requestAllWithPredicate:predicate inContext:context];
  753 + [request setPropertiesToFetch:properties];
  754 + [request setResultType:NSDictionaryResultType];
  755 + [request setPropertiesToGroupBy:[NSArray arrayWithObject:groupingKeyPath]];
  756 +
  757 + NSArray *results = [self MR_executeFetchRequest:request];
  758 + return results;
  759 +}
  760 +
  761 +
  762 +
  763 ++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath
  764 +{
  765 + return [self MR_aggregateOperation:function onAttribute:attributeName withPredicate:predicate groupBy:groupingKeyPath inContext:[NSManagedObjectContext MR_defaultContext]];
  766 +}
  767 +
  768 +#endif
  769 +
735 770 + (NSNumber *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context
736 771 {
737 772 NSExpression *ex = [NSExpression expressionForFunction:function
8 Source/MagicalRecordShorthand.h
@@ -88,8 +88,12 @@
88 88 + (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context;
89 89 + (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending;
90 90 + (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
91   -+ (NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context;
92   -+ (NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate;
  91 +#ifdef __IPHONE_5_0
  92 ++ (NSArray *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath;
  93 ++ (NSArray *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath inContext:(NSManagedObjectContext *)context;
  94 +#endif
  95 ++ (NSNumber *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context;
  96 ++ (NSNumber *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate;
93 97 - (id) inContext:(NSManagedObjectContext *)otherContext;
94 98 - (id) inThreadContext;
95 99

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.