6

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

David Ravetti
  • 2,030
  • 1
  • 17
  • 22
Josh Sherick
  • 2,161
  • 3
  • 20
  • 37

2 Answers2

7

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.

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • Wow!! Thanks, I am trying that. I am a little confused about the inverses though. For Patient to Checkpoint, is the inverse "Checkpoint"? And from Checkpoint to Patient, is the inverse just "Patient", or is there no inverse? And if i understood what you said, both values are nullify for checkpoint and patient. – Josh Sherick Jun 17 '11 at 05:14
  • Actually, hang on I'm confused myself. However, there should only be one menu option which is correct. – Moshe Jun 17 '11 at 05:34
  • what do you mean by "there should only be one menu option which is correct?" – Josh Sherick Jun 17 '11 at 05:44
  • Assuming that you are in Xcode 4. After creating both relationships without specifying an inverse relationship, the inverse dropdowns will populate with a single option. Those options will be correct. – Moshe Jun 17 '11 at 05:46
  • See my edit ("In other words..."). That might be easier to understand. – Moshe Jun 17 '11 at 05:57
2

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.

Swapna
  • 2,245
  • 2
  • 19
  • 22