3
votes

I'm running into an issue with my core data model. I would like to have an entity called TherapySession have two Mood events - start and end. To do so I've defined two relationships to child objects:

enter image description here

However, I would also like to create an inverse relationship, where each Mood object would be aware of it's parent therapy session. I'm not sure how to properly create the inverse relationship between the child and parent object when there are more than one relationship of the same type defined. In my case, the inverse relationship points to the "startMood" property of the therapy session:

enter image description here

It seems that I'm doing something wrong, but I cannot put my finger on how to resolve this problem. If I add an end mood to therapy, and the core data would try to create an inverse relationship, would it overwrite the startMood relationship?

Thank you for any clarifications! I know that this can be avoided by adding a set of objects, and then sorting the set by date, but I would like to avoid having to do that for every object.

2

2 Answers

3
votes

One solution is to stick with a simple many-to-one relationship mood and an additional attribute in entity Mood that indicates start or end.

This is also more flexible - in the future it would be trivial to introduce more moods at different therapy points etc. without having to change the data model.

2
votes

I'm not sure if this is the right way to do this, but I always end up creating two inverse relationships, like startMoodInverse and endMoodInverse. (You can then add a property in code that returns whichever of those is non-nil as therapySession.)