核心数据:通过特定属性获取(连接关系)

发布于 2024-09-19 09:29:02 字数 425 浏览 4 评论 0原文

我有一个核心数据模型,如下

alt text

Page< 的 attributes 属性/code> 是一组 DictionaryEntry,它们是我的 Page 对象的值,很像标准的 NSDictionary (除了所有的 keyvalues 是字符串)

我有一个 Page,它有一个带有 key="title"DictionaryEntry值=“主页”。我如何形成一个获取请求来加载该特定页面?

I have an core data model as follows

alt text

The attributes property of Page is a set of DictionaryEntry, they are values for my Page objects, much like a standard NSDictionary (except all of the keys and values are strings)

I have a Page that has a DictionaryEntry with key="title" and value="Home". How would i form a fetch request to load that specific page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-09-26 09:29:02

您应该查看子查询的谓词语法。您不能使用通常的 ANY 关键字,因为这仅允许您匹配一列,而不是同时匹配两列。

  NSString *keyValue = @"title";
  NSString *valueValue = @"home";

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"Page" inManagedObjectContext:_context]];
  [request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(attributes, $a, $a.key == %@ && $a.value == %@).@count != 0)", keyValue, valueValue]];

更简单的谓词 ANY attribute.key = "title" AND ANY attribute.value = "home" 不起作用,因为它还返回具有两个字典的页面,例如 key='addr'/value='home ' 且键='标题'/值='皮特'。

You should have a look at the predicate syntax for subqueries. You can not use the usual ANY keyword as this only allows you to match one column not two at the same time.

  NSString *keyValue = @"title";
  NSString *valueValue = @"home";

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"Page" inManagedObjectContext:_context]];
  [request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(attributes, $a, $a.key == %@ && $a.value == %@).@count != 0)", keyValue, valueValue]];

The simpler predicate ANY attributes.key = "title" AND ANY attributes.value = "home" would not work as it also returns pages that have two dicts e.g. key='addr'/value='home' and key='title'/value='pete'.

风透绣罗衣 2024-09-26 09:29:02

假设您首先知道“Title”的 DictionaryEntry 的“key”。

当您创建 NSPredicate 时,为什么不使用您已知的“键”在 DictionaryEntry 对象上创建 NSFetchRequest。

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"DictionaryEntry" inManagedObjectContext:_context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"key == %@", keyValue]];

一旦你有了有效的 DictionaryEntry 对象,你就可以使用 CoreData 的“页面”关系来获取有效的页面。

Assuming you start by knowing the "key" to the DictionaryEntry of "Title".

Why don't create an NSFetchRequest on the DictionaryEntry object using your known "key" when you create an NSPredicate.

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"DictionaryEntry" inManagedObjectContext:_context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"key == %@", keyValue]];

Once you have your valid DictionaryEntry object, you can then use your CoreData crufted up "page" relationship to get your valid Page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文