Why can't I create an inverse relationship in CoreData?

The problem

I want to set a relationship between two entities and I want it both to have an inverse relationship. However I couldn’t set the inverse relationship in Xcode.

For example, given the following scenario where I have Movie entity and Actor entity. A Movie should casts multiple Actor, so I want the entity to have a cast relationship between the Actor relationship.

Here’s the graph editor to help you visualize it.

core data graph consist of “Movie” entity and “Actor” entity

However when I try to set the cast relationship in the editor, I’m not able to set the inverse relationship since the option is not available to me.

core data editor with no inverse entity checked

How do I fix this?

The fix

Apparently, I have to set the relationship in the Destination entity first to have the option for inverse relationship. In this case, I need to set up a movies relationship in the Actor entity first. Like so

Actor entity with movies relationship set

Once I go back to the Movie entity’s `cast` relationship, there should be an option to set the inverse relationship to the movies relationship that I defined earlier in Actor entity.

movies inverse shown in cast relationship

Personally, I feel like this is a lot of work. Luckily I discovered that if you set the relationship through the Graph editor, it will do the work for you. All you have to do CTRL drag to the destination entity. Like so:

setting up the core data relationship through Graph editor

Then, rename the relationship to cast and movies respectively. That’s it.

References:

  1. https://developer.apple.com/documentation/coredata/modeling_data/configuring_relationships
  2. https://stackoverflow.com/questions/15657173/how-to-set-an-inverse-relationship-in-xcode