Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added grouping version of aggregate methods #113

Closed
wants to merge 9 commits into from

4 participants

@duanefields

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

@tonyarnold
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

@KingOfBrian

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!

@casademora
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 :(

@KingOfBrian

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:

@tonyarnold
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!

@tonyarnold tonyarnold closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 16, 2011
  1. @duanefields

    silence compiler warning

    duanefields authored
  2. @duanefields

    added new group-by aggregate methods

    duanefields authored
    returns dictionaries of grouped results
Commits on Dec 17, 2011
  1. @duanefields
Commits on Dec 18, 2011
  1. @duanefields

    ignoring performSelector-leak warnings only as needed

    duanefields authored
    instead of disabling the performSelector-leak warning for the whole category, pushing this onto the clang diagnostic stack as needed
  2. @duanefields

    Merge branch 'master' of https://github.com/magicalpanda/MagicalRecord

    duanefields authored
    Conflicts:
    	Source/Categories/NSManagedObject+MagicalDataImport.m
Commits on Jan 12, 2012
  1. @duanefields
Commits on Feb 28, 2012
  1. @duanefields
Commits on Mar 1, 2012
  1. @duanefields
Commits on Mar 7, 2012
  1. @duanefields
This page is out of date. Refresh to see the latest.
View
1  README.md
@@ -154,6 +154,7 @@ There are also counterpart methods which return NSUInteger rather than NSNumbers
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];
+ NSArray* caloriesByMonth = [CTFoodDiaryEntry aggregateOperation:@"sum:" onAttribute:@"fatColories" withPredicate:predicate groupBy:@"month"];
#### Finding from a different context
View
3  Source/Categories/NSManagedObject+MagicalDataImport.m
@@ -164,6 +164,8 @@ - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary
SEL shouldImportSelector = @selector(shouldImport:);
BOOL implementsShouldImport = [self respondsToSelector:shouldImportSelector];
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if (![self MR_importValue:relatedObjectData forKey:relationshipName])
{
if ([relationshipInfo isToMany])
@@ -185,6 +187,7 @@ - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary
}
}
}
+#pragma clang diagnostic pop
}
}
View
4 Source/Categories/NSManagedObject+MagicalRecord.h
@@ -99,6 +99,10 @@
+ (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending;
+ (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
+#ifdef __IPHONE_5_0
++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath inContext:(NSManagedObjectContext *)context;
++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath;
+#endif
+ (NSNumber *)MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context;
+ (NSNumber *)MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate;
View
35 Source/Categories/NSManagedObject+MagicalRecord.m
@@ -732,6 +732,41 @@ - (id) MR_objectWithMinValueFor:(NSString *)property
return [self MR_objectWithMinValueFor:property inContext:[self managedObjectContext]];
}
+#ifdef __IPHONE_5_0
+
++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath 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 MR_entityDescription] attributesByName] objectForKey:attributeName];
+ [ed setExpressionResultType:[attributeDescription attributeType]];
+ NSArray *properties = [NSArray arrayWithObjects:groupingKeyPath, ed, nil];
+ MR_RELEASE(ed);
+
+ NSFetchRequest *request = [self MR_requestAllWithPredicate:predicate inContext:context];
+ [request setPropertiesToFetch:properties];
+ [request setResultType:NSDictionaryResultType];
+ [request setPropertiesToGroupBy:[NSArray arrayWithObject:groupingKeyPath]];
+
+ NSArray *results = [self MR_executeFetchRequest:request];
+ return results;
+}
+
+
+
++ (NSArray *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath
+{
+ return [self MR_aggregateOperation:function onAttribute:attributeName withPredicate:predicate groupBy:groupingKeyPath inContext:[NSManagedObjectContext MR_defaultContext]];
+}
+
+#endif
+
+ (NSNumber *) MR_aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context
{
NSExpression *ex = [NSExpression expressionForFunction:function
View
8 Source/MagicalRecordShorthand.h
@@ -88,8 +88,12 @@
+ (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context;
+ (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending;
+ (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
-+ (NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context;
-+ (NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate;
+#ifdef __IPHONE_5_0
++ (NSArray *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath;
++ (NSArray *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate groupBy:(NSString*)groupingKeyPath inContext:(NSManagedObjectContext *)context;
+#endif
++ (NSNumber *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context;
++ (NSNumber *) aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate;
- (id) inContext:(NSManagedObjectContext *)otherContext;
- (id) inThreadContext;
Something went wrong with that request. Please try again.