【问题标题】:setPropertiesToFetch returns 'Invalid keypath expression' errorsetPropertiesToFetch 返回“无效的键路径表达式”错误
【发布时间】:2016-05-17 18:23:51
【问题描述】:

我正在尝试以最基本的形式使用 group-by 核心数据功能:

- (NSFetchedResultsController *)fetchedResultsController {

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSExpression *reviewNumKeyExpression = [NSExpression expressionForKeyPath:@"review_num"];
    NSExpression *reviewMaxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:reviewNumKeyExpression]];

    NSExpressionDescription *maxReviewED = [[NSExpressionDescription alloc] init];
    [maxReviewED setName:@"maxReview"];
    [maxReviewED setExpression:reviewMaxExpression];
    [maxReviewED setExpressionResultType:NSInteger32AttributeType];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Group" inManagedObjectContext:managedObjectContext];

    [fetchRequest setResultType:NSDictionaryResultType];
    [fetchRequest setReturnsDistinctResults:YES];
    [fetchRequest setEntity:entity];

    NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
    NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
    [fetchRequest setPropertiesToFetch:propertiesToFetch];
    [fetchRequest setPropertiesToGroupBy:propertiesToFetch];
...

运行此代码时,我收到以下错误:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'无效的键路径表达式 ((), 名称 maxReview, isOptional 1, isTransient 0, entity (null), renamingIdentifier maxDepartment、验证谓词 ()、警告 ()、 versionHashModifier (null) userInfo { }) 传递给 setPropertiesToFetch

我想我在 API 上遗漏了一些东西,所以非常感谢任何帮助!

【问题讨论】:

    标签: ios core-data


    【解决方案1】:

    我认为错误信息是错误的:问题出在setPropertiesToGroupBy,而不是setPropertiesToFetch。您不能按计算值 (maxReview) 分组,但我怀疑您是否真的想要:如果您想要每个 groupId 的最大值 review_num,您只需要 groupId 中的 propertiesToGroupBy:

    NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
    NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
    NSArray *propertiesToGroupBy = [NSArray arrayWithObjects:groupId, nil];
    [fetchRequest setPropertiesToFetch:propertiesToFetch];
    [fetchRequest setPropertiesToGroupBy:propertiesToGroupBy];
    

    【讨论】:

    • 感谢您的回答。但是,由于某种原因,您似乎还必须在 group by 子句中拥有所有获取的属性,请参阅:stackoverflow.com/questions/31935618/…
    • 好的,所以看来我确实需要向 group-by 提供所有 fetch 属性,除了计算值(maxReview)......否则我得到:由于未捕获的异常而终止应用程序'NSInvalidArgumentException ', reason: ' 带有 GROUP BY 组件的查询中的 SELECT 子句只能包含在 GROUP BY 或聚合函数中命名的属性 ((), name group_name, isOptional 1, isTransient 0, entity Group, renamingIdentifier group_name , 验证谓词
    猜你喜欢
    • 2016-03-28
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2020-12-22
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多