1

Apple's documentation suggests the use of inverse relationships when modelling data models in CoreData.

I have the following example:

A Book (Entity) has several "pages" and one "frontCover" and one "backCover". A Page (Entity) is in one "book" (so "book" is the inverse of "pages").

OK so far, that's the standard case...BUT now, my problem:

I only have one class Cover (Entity). A Cover (Entity) is on one "book". On this "book" the Cover is EITHER the "frontCover" OR the "backCover". So, the inverse of "book" is EITHER "frontCover" OR "backCover".

This cannot be modelled in CoreData. A relationship can only be the inverse of one relationship, but not of EITHER this OR that relationship.

What is the best way to model this? Unidirectional relationships (no invers)?

Thanks for your answers, Chris

3 Answers 3

2

You could do something like the model below (first image). This would leaving of Cover's inverse relationships as nil. This doesn't feel right to me, though.

Another option (second image) would be to give Book a 'covers' relationship which references 2 Cover objects, and give Cover an isFront boolean attribute. This would allow for inverse relationship called 'book'.

enter image description here

enter image description here

1
  • Hmm... I somehow like the second option. However, the display of all frontCovers of all books would be much more difficult (could be done in one operation in the first case). Moreover, I would have to make sure that Book.covers has 2 and only 2 covers of which only one is a front cover. .... Option 1 does feel a bit strange but in fact does what I want... Maybe I will go with this option ... or maybe I will stick with a unidirectional relation from book to cover.
    – naeger
    Apr 12, 2011 at 18:05
2

One way to do it could be to create Cover as an "abstract" entity with two sub entities - FrontCover & BackCover. Then you could create the relationship & inverse to each of those.

1
  • Yes, in general I agree. Usually, there is at least some difference between a FrontCover and a BackCover, so this would be a justification for two subclasses .... However, in my special case (the book and cover is only a trimmed down toy example), there is absolutely no difference between front and back covers. Even worse, in some cases the front cover can become a back cover and vice versa. So this would speak against this solution in my special case...
    – naeger
    Apr 12, 2011 at 17:54
0

I just want to add.

One way is to have 2 subentities. But that's useful only if FrontCover and BackCover differs a lot.

If they are exactly the same object, you should instead use an enum in the entities.

That enum differentiate whether the cover is frontCover or BackCover.

Then you set only 1 "to many" relationship from book to cover.

The purpose of coredata is to save your data. Your logic should be in the code anyway.

Also creating two subEntities is essensially the same with westSider's answer. Sub entities simply add another relationship on the original one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.