1

I have entities Image and Post. Post has a uni-directional Nullify relationship to Image. It's a relationship and not an attribute because I want to benefit from lazy loading of relationships, making initial fetches of Post entities very fast. As you can guess, Image contains image data.

Apple generally discourages uni-directional relationships (Link) unless there a good reason not to have an inverse relationship.

My reason for not having it is Image is a generic entity, so having inverse relationships to all entities that use it seemed like overkill. However, I wonder if there is a better way to go about this without violating Apple's recommendation.

Morrowless
  • 6,856
  • 11
  • 51
  • 81

2 Answers2

4

Parent entities.

enter image description here

Comment, BlogPost and Tweet all inherit the images relationship. And the inverse relationship points to a TextContent object, which can be a Comment, BlogPost, Tweet or TextContent itself.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Good, clean approach where I can maintain inverse relationships. – Morrowless May 21 '15 at 08:10
  • It's a nice and clean solution that I have been considering introducing as well, but I keep hearing that inheritance in Core Data is inefficient as it will inflate the underlying database because all attributes (of parents+children) will end up in the same table and cost a lot of space? – Philip De Vries Nov 03 '18 at 14:59
0

As Apple says... Unidirections Relationships

It is not strictly necessary to model a relationship in both directions

BTW I think attributes also could be loaded lazily (by system) if CoreData detects that is better to do this...

You can be interested in this then to avoid warnings in your project

Community
  • 1
  • 1
Bisca
  • 6,380
  • 2
  • 19
  • 32