iPhone开发-使用CoreData查询相关记录

发布于 2024-08-28 11:32:06 字数 458 浏览 10 评论 0原文

我有一个案例,我有三个具有一对多和一对多关系的实体:

Entity A (Entity B relationhip), 
Entity B (Entity A relationship, Entity C relationship),
Entity C (Entity B relationhip)

我有实体 A 的引用,现在我想获取所有相关的实体 C 记录。我怎样才能做到这一点? (用最少的代码)

编辑:这是另一种表达方式。

我们可以与 CoreData 执行连接吗?例如,(这是一个非常粗略的例子),我们有一个以下实体关系:

Grand Parent  (1)---(m)  Parent
Parent        (1)---(m)  Child

那么,现在如果我有“阿尔伯特”祖父母,并且我想得到他所有的孙子,我该怎么做?

I have a case where i have three entities with one-to-many and one-to-many relationships:

Entity A (Entity B relationhip), 
Entity B (Entity A relationship, Entity C relationship),
Entity C (Entity B relationhip)

I have the reference of Entity A, and now i want to fetch all the related Entity C records. How can i do that? (with least amount of code)

Edit: Here's another way to put it.

Can we perform joins with CoreData. For example, (and this is a very crude example), We have a following entity-relationship:

Grand Parent  (1)---(m)  Parent
Parent        (1)---(m)  Child

So, now if i have "Albert" the Grand Parent, and i want to get all his grand children, how can i do that?

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

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

发布评论

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

评论(1

猫腻 2024-09-04 11:32:06

如果其他人偶然发现类似的情况,这对我有用:

NSArray *allFieldValues = [myEntityA valueForKeyPath:@"Entity B relationship.Entity C relationship.requiredFieldInEntityC"];

我主要感兴趣的是读取实体 C 中单个字段的数据(链接到 myEntityA 对象)。这里的关键概念是“不要将 CoreData 视为‘数据库’”。

In case someone else stumble across a similar situation, here's what worked for me:

NSArray *allFieldValues = [myEntityA valueForKeyPath:@"Entity B relationship.Entity C relationship.requiredFieldInEntityC"];

I was mainly interesting in reading the data of a single field in Entity C (that's linked to myEntityA object). The key concept here is that "don't think of CoreData as a 'database'".

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