4

Possible Duplicate:
Does every Core Data Relationship have to have an Inverse?

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?

Community
  • 1
  • 1
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

1 Answers1

5

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

hooleyhoop
  • 9,128
  • 5
  • 37
  • 58
  • 1
    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 Jun 04 '10 at 15:43
  • 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 Jun 04 '10 at 16:55
  • @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. – hooleyhoop Jun 04 '10 at 19:32