【问题标题】:Core Data: How to fetch Entities based on an attribute of a related entity核心数据:如何根据相关实体的属性获取实体
【发布时间】:2015-03-14 16:10:57
【问题描述】:

这是我的对象图的相关部分的样子:

[Anime] <->> [AnimeName @string @type]

所以一个Anime 对象有许多AnimeName 对象,其中包含一个字符串和它们的名称类型。现在要实现“按名称搜索”功能,我需要一个匹配所有Anime 实体的谓词,其中任何名称都包含搜索字符串。到目前为止,我尝试过的是:

NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Anime"];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"names.string == %@", searchString]];

Anime 实体中的NSFetchRequest 上,这会产生以下错误:

"NSInvalidArgumentException", "to-many key not allowed here"

这对我来说很有意义,我可以通过使用这个来解决这个问题:

NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"AnimeName"];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"string == %@", searchString]];

然后从每个返回的对象中获取与Anime 实体的关系,但是如何将其插入到NSFetchedResultsController 中以用于我的UITableView

如果有人知道解决方案,请提供帮助。

【问题讨论】:

    标签: ios core-data nspredicate nsfetchedresultscontroller nsfetchrequest


    【解决方案1】:

    对于多对关系,使用“ANY”:

    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Anime"];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"ANY names.string == %@", searchString]];
    

    【讨论】:

    • 谢谢,就像一个魅力,出于某种原因,我从来没有想过使用这样的任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多