Basic data: saving an array of objects

I'm new to Core Data, and I was wondering if I can get some recommendations on how to best lay out the following scenario with Core Data:

I have a patient object (and its corresponding subclass NSManagedObject). Each patient may have various diseases. Each disease is its own essence and controlled object. In my patient class, I want to have an array filled with diseases for this patient. However, Core Data does not allow you to store NSArray as an attribute.

What would be the best way to organize this in Core Data?

I thought of some options:

  • Use portable attribute in Patient object and store array in this? Doesn't seem very clean though

  • Using an intermediate Controller object between a patient and diseases that can mimic some array functions

  • I don’t know if this is possible, but perhaps to sample and get only those diseases that are associated with a particular patient?

Thanks for the help!

+7
source share
1 answer

That's what relationships are for.

The patient has many diseases.
Disease has_one Patient

Define them, and then you can do this:

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

Read more about it here .

+7
source

All Articles