One-to-many relationship modeling in Core Data for iOS

I have two objects: a patient and a checkpoint. The patient has attributes such as DOB, name, identifier, etc. The checkpoint has attributes such as dateRecorded, height, weight, etc.

You probably have an idea: I want to have a set of patients there, and each patient could have control points associated with this patient.

On both objects, how do I configure the settings? Settings: Relationship Window

I was looking at the documentation for this, and I was still confused. I think I want from one to many relationships (for patients), but then I’m not sure how to establish the opposite for any of them, or the rules for deletion and other things. THANKS!!

+6
source share
2 answers

I just started working with Core Data this week. The big question!

Relations:

Because one patient can have many control points, the Patient to Checkpoint relationship is the One to Many relationship. The concept of “feedback” is basically this: you have a relationship going one way (Patient to Checkpoint) - now go and look at it from the back, at the control point. The control point can be applied to only one patient. Therefore, the Checkpoint to Patient relationship is a One to One relationship.

Inverse relationship:

To handle the feedback, simply create each relationship, ignoring the opposite. Then, after you have a relation to each object, go ahead and define the opposite as a relation to another object.

In other words, a relation points to another object or group of entities. An inverse relationship indicates a relationship to another object.

Delete rules:

As for the removal rules, this is pretty simple. When trying to remove a patient who has milestones ...

  • Deny: These kernels will not allow you to delete a patient.
  • Cascade: Core Data will remove Entity (Patient), and also cascade through relationships and delete these objects. (In other words, Core Data will also remove checkpoint objects.)
  • Nullify: Core Data will delete the patient, but the relationship will be deleted first. The control point will remain untouched.

The Patient object may require either a failure or a cascade, depending on how you want to manage your data. Based on your use case, you probably do not want to reset the value, since the control points depend on the patient’s objects.

You want to invalidate the control point, because Cascade will not allow you to delete the control point without deleting the entire patient, and Deny will effectively force it.

+7
source

Based on the scenario described above, it looks like a one-to-many relationship between patients and checkpoint tables.

Now add the relationship from Patient to Control Point, and also set the opposite between the tables.

Also set the delete rule for both relationships for the cascade. This means that if you delete one object using Patient, Corcelact Corressata will also delete the associated object.

+2
source

Source: https://habr.com/ru/post/890727/


All Articles