Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the best variations on this theme are (not necessarily in order):</p> <ol> <li><p>Use separate entities for the pictures associated with Meal, Person, Boardroom, etc. Those entities might all have the same attributes, and they might in fact all be implemented using the same class. There's nothing wrong with that, and it makes it simple to have a bidirectional relationship between each kind of entity and the entity that stores its picture.</p></li> <li><p>Make the picture an attribute of each of the entity types rather than a separate entity. This isn't a great plan with respect to efficiency if you're storing the actual picture data in the database, but it'd be fine if you store the image as a separate file and store the path to that file in an attribute. If the images or the number of records is small, it may not really be a problem even if you do store the image data in the database.</p></li> <li><p>Use a single entity for all the pictures but omit the inverse relationship back to the associated entity. There's a <a href="https://stackoverflow.com/q/764125/643383">helpful SO question</a> that considers this, and the accepted answer links to the even more helpful <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW6" rel="nofollow noreferrer"><em>Unidirectional Relationships</em></a> section of the docs. This can be a nice solution to your problem if you don't need the picture->owner relationship, but you should understand the possible risk before you go down that road.</p></li> <li><p>Give your picture entity separate relationships for each possible kind of owner, as you described in the first option you listed. If you'll need to be able to access all the pictures as a group and you need a relationship from the picture back to its owner, and if the number of possible owner entities is relatively small, this might be your best option even if it seems sloppy to have empty attributes.</p></li> </ol> <p>As you noticed, when you use inheritance with your entities, <a href="https://stackoverflow.com/q/2321003/643383">all the sub-entities end up together in one big table</a>. So, your fourth option (using sub-entities for each kind of picture) is similar under the hood to your first option.</p> <p>Thinking more about this question, I'm inclined toward using entity inheritance to create subentities for the pictures associated with each type of owner entity. The Picture entity would store just the data that's associated with any picture. Each subentity, like MealPicture and PersonPicture, would add a relationship to it's own particular sort of owner. This way, you get bidirectional Meal&lt;->MealPicture and Person&lt;->PersonPicture relationships, and because each subentity inherits all the common Picture stuff you avoid the DRY violation that was bugging you. In short, you get most of the best parts of options 1 and 3 above. Under the hood, Core Data manages the pictures as in option 4 above, but in use each of the picture subentities only exposes a single relationship.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload