核心数据获取记录按关系分组? [英] Core data fetch records Group by relationship?

查看:116
本文介绍了核心数据获取记录按关系分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体名为费用费用类别 费用 CategoryExpenses 具有以下属性:
name-expenseCategory,Destination - ExpensesCategory,inverse - expense。
ExpensesCategory 有一个属性名为expenseCategory的字符串类型,费用有一个属性名为amount。
现在我想获取费用按费用分组,总金额。
这里是我的代码

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];

但是当我运行我得到终止应用程序由于未捕获异常'NSInvalidArgumentException' ,reason:'传递给setPropertiesToFetch的属性/关系描述名称必须与获取实体匹配的名称

任何帮助都不胜感激。 >

Any help would be appreciated.

推荐答案

这是您的问题的答案。

NSEntityDescription *entity = [NSEntityDescription entityForName:@"ExpenseCategory" inManagedObjectContext:context];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];

NSError *error;

fetchRequest.predicate =[NSPredicate predicateWithFormat:@"(addedTimestamp <= %@) AND (addedTimestamp >= %@)",currentDate, endDate];

NSSortDescriptor *sortTitle =[NSSortDescriptor sortDescriptorWithKey:@"addedTimestamp" ascending:YES];
fetchRequest.sortDescriptors  = [NSArray arrayWithObjects:sortTitle, nil];

NSExpressionDescription *ex = [[NSExpressionDescription alloc] init];
[ex setExpression:[NSExpression expressionWithFormat:@"@sum.expense.amount"]];
[ex setName:@"Sum"];
[ex setExpressionResultType:NSDecimalAttributeType];

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"expenseCategory", ex, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@"expenseCategory"]];
[fetchRequest setResultType:NSDictionaryResultType ];

// Execute the fetch.
NSArray *matchingObjects = [context executeFetchRequest:fetchRequest error:&error];
if ([matchingObjects lastObject]) {
    for (id expenseCategory in matchingObjects) {
        NSLog(@"%@", expenseCategory);
    }
}

如果您想按Expense addedTime筛选ExpenseCategory addedTime,然后将谓语更改为:

If you are wanting to filter by Expense addedTime and not the ExpenseCategory addedTime, then change the predicate to:

fetchRequest.predicate =[NSPredicate predicateWithFormat:@"(expense.addedTimestamp <= %@) AND (expense.addedTimestamp >= %@)",currentDate, endDate];

这篇关于核心数据获取记录按关系分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆