在 iOS 的 Core Data 中建模一对多关系

发布于 2024-11-16 00:33:47 字数 429 浏览 2 评论 0原文

我有两个实体:患者和检查点。 患者具有出生日期、姓名、ID 等属性。 检查点具有记录日期、身高、体重等属性。

您可能明白了——我希望有一组患者,然后每个患者都可以有与该患者关联的检查点。

在这两个实体上,我应该如何设置?设置为: 关系窗口

我查看了 文档 对此,我仍然感到困惑。我认为我想要的是一对多关系(对于患者),但是我不确定如何为它们中的任何一个或删除规则和其他内容设置逆关系。谢谢你!!

I have two entities: patient and checkpoint.
Patient has attributes such as DOB, name, ID, etc.
Checkpoint has attributes such as dateRecorded, height, weight, etc.

You probably get the idea- I want there to be a set of patients, and then each patient can have checkpoints associated with that patient.

On both entities, how should I set the settings? The settings are:
Relationship Window

I looked at the documentation for this, and I was still confused. I think what I want is a one to many relationship (for patient), but then I'm not sure how to set the inverses for either of them, or the delete rule and the other stuff. THANK YOU!!

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

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

发布评论

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

评论(2

↘紸啶 2024-11-23 00:33:47

我本周刚刚开始使用核心数据。好问题!

关系

由于一名患者可以有多个检查点,因此患者与检查点关系是一种一对多关系。 “反向关系”的概念本质上是这样的:您已经建立了一种单向关系(患者与检查点) - 现在继续从反向(检查点的角度)来看待它。一个检查点只能适用于一名患者。因此,检查点与患者关系是一种一对一关系。

逆向关系:

要处理逆向关系,只需创建每个关系,忽略逆向关系。然后,在获得每个对象上的关系后,继续将逆关系定义为另一个实体上的关系。

换句话说,关系指向另一个实体或一组实体。 反向关系指向另一个实体上的关系。

删除规则:

就删除规则而言,它相当简单。当尝试删除具有检查点的患者时...

  • 拒绝:核心数据不会让您删除患者。
  • 级联:核心数据将删除实体(患者),并通过关系级联并删除这些对象。 (换句话说,Core Data 也会删除检查点对象。)
  • Nullify:Core Data 将删除患者,但首先删除关系。检查站将保持完好无损。

对于患者实体可能需要拒绝或级联,具体取决于您想要如何管理数据。根据您的使用案例,您可能不希望无效,因为检查点依赖于患者实体。

您希望检查点无效,因为级联会阻止您在不删除整个患者的情况下删除检查点,而拒绝会有效地强制执行相同操作。

I just got started with Core Data this week. Great question!

Relationships:

Since one patient can have many checkpoints, the Patient to Checkpoint relationship is a One to Many relationship. The concept of an "inverse relationship" is essentially this: You've got a relationship going one way (Patient to Checkpoint) - now go ahead and look at it from the inverse, the Checkpoint's perspective. A checkpoint can apply to only a single patient. Therefore, the Checkpoint to Patient relationship is a One to One relationship.

Inverse Relationships:

To handle the inverse relationship, simply create each relationship, ignoring the inverse. Then, after you have the relationship on each object, go ahead and define the inverse as the relationship on the other entity.

In other words, a relationship points to another entity or a group of entities. An inverse relationship points to a relationship on another entity.

Delete Rules:

As far as delete rules are concerned, it's fairly simple. When trying to delete a patient which has checkpoints...

  • Deny: Core Data won't let you delete the Patient.
  • Cascade: Core Data will delete the Entity (Patient), as well as cascading through relationships and deleting those objects as well. (In other words, Core Data will delete the Checkpoint objects too.)
  • Nullify: Core Data will delete the patient but first remove the relationship. The Checkpoint will remain intact.

For the Patient entity might want either deny or cascade, depending on how you want to manage your data. Based on your usage case, you probably don't want nullify, since Checkpoints are dependent upon Patient entities.

You want nullify for the Checkpoint, since the Cascade would prevent you from deleting a checkpoint without deleting the entire patient, and Deny would effectively force the same.

︶ ̄淡然 2024-11-23 00:33:47

根据所提到的场景,它看起来像是患者表和检查点表之间的一对多关系。

现在添加从“Patient”到“Checkpoint”的关系,并设置表之间的逆关系。

另外,将两个关系的删除规则设置为“级联”。这意味着,如果您删除带有 Patient 的一个对象,相应的 Coredata 也会删除关联的对象。

Based on the scenario mentinoed, it looks like a one to many relationship between patient and checkpoint tables.

Now add a relationship from “Patient” to “Checkpoint”, and also set the inverse between the tables.

Also, set the delete rule for both relationships to “cascade”. This means that if you delete one object with Patient, the corressponding Coredata will delete the associated object as well.

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