7

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!

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick answer. The thing is that I was planning on making each decease a separate entity and class. I don't have a decease entity for all the deceases as their proerties vary a lot from eahc other. I could do a relationship between Patient and each different decease but i don't know how to fetch these relationships into an array or set.
Make a generic decease entity and set up the relationships like I mentioned. Then create your real decease entities inherit from the original. You'll have to be careful when accessing properties, but in general you'll be able to get all of the inherited deceases from that one relationship with the parent decease.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.