views:

108

answers:

3

Example: If I had two entities Person and Car, and each of them should be linked with an Image entity - how would this have to look like?

Right now, I would make just three Entities: Person, Car, Image. So persons and cars can link to an Image entity. They have a 1:1 relationship to the Image entity.

But now, a Core Data dude said that relationships have to be modeled both ways. That's stupid or not? I need two Image entities then. CarImage and PersonImage. Or one weird Image entity which has two relationships: One to a Car entity, and one to a Person entity.

How would you model that?

+1  A: 

I think if you don't plan to use the reversed relationship in your code it doesn't make sense to define it. It makes even less sense if you need to define new entities just to reverse it.

RaYell
You always need bi-directional relationships so that Core Data can maintain referential integrity.
Marcus S. Zarra
A: 

The issue isn't that relationships need to be bidirectional; it's that if you want bidirectional relationships, you have to model them both ways. If the image entity can't be used to look up a Car or Person entity, then no need for the reverse relationship, and no need to model it. (This can, to a certain degree, be mitigated by having a super-class for your entities, but that's a story for another day.)

Williham Totland
Actually it is a **need** for Core Data. Without bidirectional relationships a lot of things perform worse or don't work at all.
Marcus S. Zarra
+3  A: 

The brief version..

Yes the relationships need to be modelled both ways. No that doesn't mean you need two Image entities. If a person had a one-way relationship to an image, and you deleted the image (which doesn't have an inverse relationship, so knows nothing of the person) your graph is now corrupt.

So you need two way relationships, but that doesn't mean you need two image entities. Your Image just needs one relationship - 'container', say, that could be a Person or a Car (a shared Parent entity will help).

The longer version.. https://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW6

mustISignUp
Or, if `Person` and `Car` are so different that a parent entity doesn't make sense, you could add both a `person` and `car` relationship to the `Image` entity to serve as the inverse -- especially if a `Person` and `Car` could both have relationships with the same `Image`. But using a single `container` relationship with a parent entity should be your preferred approach.
Alex
Please, can you explain what you mean by single container relationship? What's a container relationship? I assume you don't talk about some kind of join table or join entity.
dontWatchMyProfile
@mystify I just mean you need one relationship in your image entity. This one entity can serve as the inverse relationship to both Person's image and Car's Image. To do this Person and Car would need the same parent entity - say 'DisplayableItem' (You can think if a better name - Container was a poor choice in my answer). As @Alex says, this approach might not be right for you - it might not be appropriate to give Car and Person the same Parent Entity. So you could give Image 2 Relationships, a Person and a Car, and only set one of them. Either way.. you don't 'Need' 2 Image Entities.
mustISignUp