I’m not sure if I’m using the correct term here, but let’s say I have four entities in my model: Person, Place, Tag, and Photo. Any of the other three entities can have a Photo related to it. Sometimes a Person will have taken the photo and will attach it to a Tag, or a Place, or even another Person. What is the best way to approach polymorphic associations in CoreData?
I’m not sure if I’m using the correct term here, but let’s say I
Share
I do not recommend entity inheritance for something as simple as this. For this design you have three relationships, each of the
Person,PlaceandTaghave a relationship toPhoto.Photo` has three inverse relationships. It does not need to be any more complicated than that.Entity inheritance is a very delicate tool that can easily cause you many problems. Any entities that inherit from another will be flattened into a single table. If, as @morningstar suggested you created a
Nounentity and havePerson,PlaceandTaginherit from it you will have ONE table in the SQLite file with indexes pointing to itself and other nastiness.When to use entity inheritence?
That is a rather hard question to answer with a simple rule. However, I would say that a baseline would be to make sure that the resulting table has at least a 70% fill. For example, if you have an abstract with 6 attributes and two children with 2 attributes then that would probably be about an 80% fill rate and potentially ok.
In general, I do not use entity inheritence as it has little benefit and a great deal of risk to performance. The old rule works, don’t use it unless you know you need to use it.