开发者

Modeling a one-to-many relationship in Core Data for iOS

开发者 https://www.devze.com 2023-03-13 19:17 出处:网络
I have two entities: patient and checkpoint. Patient has attributes such as DOB, n开发者_如何学JAVAame, ID, etc.

I have two entities: patient and checkpoint. Patient has attributes such as DOB, n开发者_如何学JAVAame, 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:

Modeling a one-to-many relationship in Core Data for iOS

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!!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号