Following Jeff Lamarch's fantastic tutorial , I am trying to aggregate data for a specific subclass of NSManagedObject .
This is the script. I created a class called Product that extends the NSManagedObject class. Product class has three properties, such as:
@property (nonatomic, retain) NSString* name; @property (nonatomic, retain) NSNumber* quantity; @property (nonatomic, retain) NSNumber* price;
I also created a category called Product+Aggregate , where I am aggregating the amount. In particular, following Jeff's study guide, I managed to get the amount for the quantity attribute.
+(NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inManagedObjectContext:(NSManagedObjectContext *)context { NSString* className = NSStringFromClass([self class]); NSExpression *ex = [NSExpression expressionForFunction:function arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:attributeName]]]; NSExpressionDescription *ed = [[NSExpressionDescription alloc] init]; [ed setName:@"result"]; [ed setExpression:ex]; [ed setExpressionResultType:NSInteger64AttributeType]; NSArray *properties = [NSArray arrayWithObject:ed]; [ed release]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setPropertiesToFetch:properties]; [request setResultType:NSDictionaryResultType]; if (predicate != nil) [request setPredicate:predicate]; NSEntityDescription *entity = [NSEntityDescription entityForName:className inManagedObjectContext:context]; [request setEntity:entity]; NSArray *results = [context executeFetchRequest:request error:nil]; NSDictionary *resultsDictionary = [results objectAtIndex:0]; NSNumber *resultValue = [resultsDictionary objectForKey:@"result"]; return resultValue; }
This class method is called the following: UIViewController :
NSNumber *totalQuantity = [Product aggregateOperation:@"sum:" onAttribute:@"quantity" withPredicate:nil inManagedObjectContext:self.context];
The code works well. In fact, if I say that 3 products
NAME QUANTITY PRICE PRODUCT 1 2 23.00 PRODUCT 2 4 12.00 PRODUCT 3 1 2.00
The aggregateOperation method returns 7 as expected.
Now I will have one more step. By changing this method, I need to return the total cost of the product order. In other words, I need to calculate the QUANTITY * PRICE value for each product and finally return TOTAL.
Could you suggest me the right way? Thank you in advance.
EDIT This is the new code that I use after the Cyberfox suggestion, but unfortunately it does not work.
NSString* className = NSStringFromClass([self class]); NSArray *quantityPrice = [NSArray arrayWithObjects: [NSExpression expressionForKeyPath:@"quantity"], [NSExpression expressionForKeyPath:@"price"], nil]; NSArray *multiplyExpression = [NSArray arrayWithObject:[NSExpression expressionForFunction:@"multiply:by:" arguments:quantityPrice]]; NSExpression *ex = [NSExpression expressionForFunction:function arguments:multiplyExpression]; NSExpressionDescription *ed = [[NSExpressionDescription alloc] init]; [ed setName:@"result"]; [ed setExpression:ex]; [ed setExpressionResultType:NSInteger64AttributeType];