Do I Really Always Need Bidirectional Relationships in Core Data?

Possible duplicate:
Should the opposite be true for each data core?

Example. If I had two Person and Car objects, and each of them should be connected to the Image object - what should it look like?

Right now, I would make only three entities: Person, Car, Image. This way, people and cars can reference the Image object. They are 1: 1 related to the Image object.

But now, Core Data dude said relationships should be modeled in both directions. Is it stupid or not? Then I need two image objects. CarImage and PersonImage. Or one weird Image object that has two relationships: from one to the auto object and one to the Person object.

How would you formulate this?

+4
source share
1 answer

Short version.

Yes, relationships need to be modeled in both directions. No, this does not mean that you need two image objects. If a person had a one-way connection with the image, and you deleted the image (which has no feedback, therefore does not know anything about the person), your schedule is now corrupted.

So, you need a two-way relationship, but that doesn't mean you need two image objects. Your image needs only one relationship - the "container", say, it can be Person or Car (a common parent will help).

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

+5
source

Source: https://habr.com/ru/post/1311862/


All Articles