A Warning about your inverse relationships
When working on a data model it is common to run into a situation where one data entity knows about another but not the other way around. Usually you can make the argument that both objects should know about each other. However, for one reason or another, which usually comes down to some underlying implementation detail or design decision, you may have a need for a one way relationship. For example, if I’m creating a blogging application (which I’m not) I might decide that a Blog entity knows about all of its associated BlogPosts but there is no need for blog posts to know about the blog that they live in (an application specific design decision). This would give rise to the following data model:

As you can see our ‘Blog’ entity knows about multiple ‘BlogPost’ entities but not the other way around. XCode refers to these other relationships as the ‘inverse relationship’. So, for our simple example there is no inverse relationship between BlogPost and Blog.
“Well, ok, so what’s the big deal?”, you may be thinking. The big deal is Warnings my friend. If you’re like me you like your code to compile without a bunch of warnings. That way when something does come up it is easily spotted. It seems that Apple really, really wants you to have those inverse relationships. In fact, they pretty much say so in the Core Data documentation:
“It is possible for relationships to be navigable in only one direction (if you are never interested in finding out from a department object what employees are associated with it, then you do not have to model that relationship), however you are strongly encouraged always to model relationships in both directions.”
In fact, Apple feels so strongly about this that your build will produce a warning for each relationship that does not have an inverse.

So much for my clean build. Or is it? A little digging reveals that the kind hearted folks at Apple thought ahead and created a build setting to turn these warnings off if you so desire. Note that they are on by default. Simply open up the build settings for your project in XCode and search for ‘momc’. The utility momc is the XCode model compiler which can also be run from the command line if needed (see the XCode User Guide for more info)

Simply check the box to ‘Suppress momc warnings on missing inverse relationships’ and you’re done. No more warnings on a data model that you know is the way you want it. How thoughtful. Props to the guy (or gal) who fought for this setting!

No Comments Yet