I've two entities named Expenses and ExpensesCategory the Expenses has a relationship with CategoryExpenses with following attributes-
name-expenseCategory, Destination - ExpensesCategory, inverse - expense.
the ExpensesCategory has an attribute named expenseCategory of string type and Expenses has an attribute named amount.
Now i want to fetch expenses Grouped by expenseCategory with sum on amount.
here is my code
NSSortDescriptor *sortTitle =[NSSortDescriptor sortDescriptorWithKey:@"addedTimestamp"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortTitle, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense"
inManagedObjectContext:context];
NSPredicate *predicate;
predicate =[NSPredicate predicateWithFormat:@"(addedTimestamp <= %@) AND (addedTimestamp >= %@)",currentDate, endDate];
NSAttributeDescription* amtDescription = [entity.attributesByName objectForKey:@"amount"];
NSRelationshipDescription* nameDesc = [entity.relationshipsByName objectForKey:@"expenseCategory"];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"amount"];
NSExpression *countExpression = [NSExpression expressionForFunction: @"sum:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: @"Sum"];
[expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSDecimalAttributeType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:amtDescription, expressionDescription, nameDesc, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:nameDesc]];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:sortDescriptors];
[context executeFetchRequest:fetchRequest error:&error];
But when i run i got Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attribute/relationship description names passed to setPropertiesToFetch: must match name on fetch entity
Any help would be appreciated.