Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 226
Josh Sherick Avatar asked Jun 17 '11 04:06

Josh Sherick


People also ask

What is Core Data in iOS?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

What is Core Data in Swift iOS?

Overview. Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is NSManagedObject in Core Data?

In some respects, an NSManagedObject acts like a dictionary—it's a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance.


2 Answers

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.

like image 125
Moshe Avatar answered Sep 28 '22 02:09

Moshe


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.

like image 40
Swapna Avatar answered Sep 28 '22 01:09

Swapna