【问题标题】:iOS Core Data NSFetchRequest groupby relationship objectiOS Core Data NSFetchRequest groupby 关系对象
【发布时间】:2013-11-29 09:37:13
【问题描述】:

我有 2 个实体:帖子和类别,帖子实体与类别有关系,每个帖子都有一个类别。

帖子类别 ---- -------- postId 类别Id 标题标题 蛞蝓 类别---->帖子

我需要的是 NSFetchRequest 按日期获取帖子,然后到 groupBy 类别。

所以结果会是这样的:

类别1: - 帖子 1 - 帖子 2 第 2 类: - 发布 3 - 帖子 4

我试过了:

[_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:@"category.categoryId", nil]];

和:

[_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:@"category", nil]];

没有成功。有什么建议吗?

谢谢。

【问题讨论】:

    标签: ios iphone core-data nsfetchedresultscontroller nsfetchrequest


    【解决方案1】:

    你能试试这样的吗:

    //context is the NSManagedObjectContext
    NSEntityDescription* post = [NSEntityDescription entityForName:@"Post"
                                                  inManagedObjectContext:context];
    NSAttributeDescription* catDesc = [post.attributesByName objectForKey:@"category"];
    [_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:catDesc, nil]];
    

    【讨论】:

    • 我试过了,让我崩溃:*** 由于未捕获的异常 NSInvalidArgumentException 导致应用程序终止,原因:使用 GROUP BY 组件的查询中的 SELECT 子句只能包含在 GROUP BY 或聚合中命名的属性函数 ((), name __af_resourceIdentifier, isOptional 1, isTransient 0, entity Post, renamingIdentifier __af_resourceIdentifier, 验证谓词 (), 警告 (), versionHashModifier (null) userInfo {}, attributeType 700, attributeValueClassName NSString, defaultValue ( null) 不在 GROUP BY)
    • im 使用 AFIncrementalStore,它插入了上面提到的属性__af_resourceIdentifier 导致崩溃
    • 对不起,只是更仔细地阅读了您的问题,也许您在这里想要的不是 groupby 而是 order by,即您按 categoryId 对帖子进行排序,然后您可以遍历它们以将其分解为组.我现在能想到的最简单的方法是在 Post Entity 中添加一个 categoryId 字段并相应地设置 sortDescriptors。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多