在 iOS 的 Core Data 中建模一对多关系
我有两个实体:患者和检查点。 患者具有出生日期、姓名、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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我本周刚刚开始使用核心数据。好问题!
关系:
由于一名患者可以有多个检查点,因此
患者与检查点
关系是一种一对多
关系。 “反向关系”的概念本质上是这样的:您已经建立了一种单向关系(患者与检查点) - 现在继续从反向(检查点的角度)来看待它。一个检查点只能适用于一名患者。因此,检查点与患者
关系是一种一对一
关系。逆向关系:
要处理逆向关系,只需创建每个关系,忽略逆向关系。然后,在获得每个对象上的关系后,继续将逆关系定义为另一个实体上的关系。
换句话说,关系指向另一个实体或一组实体。 反向关系指向另一个实体上的关系。
删除规则:
就删除规则而言,它相当简单。当尝试删除具有检查点的患者时...
对于患者实体可能需要拒绝或级联,具体取决于您想要如何管理数据。根据您的使用案例,您可能不希望无效,因为检查点依赖于患者实体。
您希望检查点无效,因为级联会阻止您在不删除整个患者的情况下删除检查点,而拒绝会有效地强制执行相同操作。
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 aOne 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, theCheckpoint to Patient
relationship is aOne 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...
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.
根据所提到的场景,它看起来像是患者表和检查点表之间的一对多关系。
现在添加从“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.