Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: storing an array of objects

I am new to Core Data and I was wondering if I could get some advice on how to best lay out the following scenario with Core Data:

I have a Patient entity (and its corresponding NSManagedObject subclass). Each patient can have various diseases. Each disease is its own entity and managed object. In my Patient class I want to have an array filled with the diseases for that patient. However Core Data does not let you store an NSArray as an attribute.

What would be the best way to go about organizing this in Core Data?

I have thought of some options:

  1. Use a transferable attribute in the Patient entity and store the array in this? Doesn't seem very clean though

  2. Use an intermediate Controller entity between the Patient and the Diseases that could simulate some of the features of an array

  3. I don't know if this is possible but perhaps do a fetch and only get those diseases that have a relationship with a certain patient?

Thank you for any help!

like image 218
Christian R Avatar asked Jun 17 '11 10:06

Christian R


1 Answers

This is what relationships are for.

Patient has_many Diseases
Disease has_one Patient

Set these up and then you can to stuff like this:

patient.diseases //returns an NSSet (very much like an array)

Read more about it here.

like image 170
sosborn Avatar answered Nov 16 '22 20:11

sosborn