首页 文章

核心数据:如何根据相关实体的属性获取实体

提问于
浏览
1

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

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

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

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

NSFetchRequestAnime 实体上,但是会出现以下错误:

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

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

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

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

如果有人知道解决方案,请帮忙 .

1 回答

  • 3

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

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

相关问题