1

I have a goods in document and I want to multiply quantity and price of every good in document and sum it.

In mySQL something like this: SELECT SUM(price * quantity) FROM documentGoods

Here is my code, but it doesn't work.

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:self.tableName inManagedObjectContext:moc]];
[fetch setResultType:NSDictionaryResultType];

NSExpression *multiplyExpression = [NSExpression expressionForFunction:@"multiply:by:" arguments:@[[NSExpression expressionForKeyPath:@"quantity"], [NSExpression expressionForKeyPath:@"price"]]];
NSExpressionDescription *expressionMultipliedDescription = [[NSExpressionDescription alloc] init];
[expressionMultipliedDescription setName:@"multiplied"];
[expressionMultipliedDescription setExpression:multiplyExpression];
[expressionMultipliedDescription setExpressionResultType:NSDecimalAttributeType];

NSExpression *sumExpression = [NSExpression expressionForFunction:@"sum:" arguments:@[[NSExpression expressionForKeyPath:@"multiplied"]]];
NSExpressionDescription *expressionSummaryDescription = [[NSExpressionDescription alloc] init];
[expressionSummaryDescription setName:@"summary"];
[expressionSummaryDescription setExpression:sumExpression];
[expressionSummaryDescription setExpressionResultType:NSDecimalAttributeType];

[fetch setPropertiesToFetch:@[expressionSummaryDescription]];
NSPredicate *searchFilter = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"removed = NO"]]];
[fetch setPredicate:searchFilter];
NSError *error = nil;
NSArray *objects = [moc executeFetchRequest:fetch error:&error];
if(objects && [objects count] > 0)
    return [[objects objectAtIndex:0] valueForKey:@"summary"];
return [[NSDecimalNumber alloc] initWithFloat:.0f];

Can anyone please help me?

imike
  • 5,515
  • 2
  • 37
  • 44
  • Stops on [moc executeFetchRequest:fetch error:&error] – imike Aug 18 '14 at 16:37
  • What does the error say? – koen Aug 18 '14 at 16:40
  • It seems that you cannot "nest" expressions in Core Data fetch requests, compare http://stackoverflow.com/questions/14122086/chained-expressions-to-perform-calculations-in-core-data (probably a "duplicate" question). – Martin R Aug 18 '14 at 16:41
  • CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Non-attribute property passed to sum: sum:(multiplied) with userInfo (null) – imike Aug 18 '14 at 16:42
  • What happens in `controllerDidChangeContent:` ? – koen Aug 18 '14 at 17:54

1 Answers1

0

I faced a similar scenario where my entity is "Cart" and has fields price and quantity. To get sum(quantity * price) from cart i used the following code. The last NSLog would log the required total cart price

NSFetchRequest *request = [[NSFetchRequest alloc] init];

request.entity = [NSEntityDescription entityForName:@"Cart" inManagedObjectContext:self.managedObjectContext];
request.resultType = NSDictionaryResultType;

NSExpressionDescription *productTotalDescr = [[NSExpressionDescription alloc] init];
[productTotalDescr setName:@"productTotal"];
[productTotalDescr setExpression:[NSExpression expressionForFunction:@"multiply:by:"
                                                    arguments:[NSArray arrayWithObjects:
                                                               [NSExpression expressionForKeyPath:@"price"],
                                                               [NSExpression expressionForKeyPath:@"quantity"],
                                                               nil]]];
[productTotalDescr setExpressionResultType:NSInteger64AttributeType];

request.propertiesToFetch = [NSArray arrayWithObjects:productTotalDescr, nil];

NSError *err = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&err];
NSLog(@"%@",[results valueForKeyPath:@"productTotal"]);
Srinivasan
  • 504
  • 6
  • 8