Official documentation recommends setting up a inverse for each relationship that can be set inverse. The goal is to keep the database correct. However, there is no specific explanation.
I found a good answer in the StackOverflow, http://stackoverflow.com/questions/764125/. Does-every-core-data-relationship-have-to-have-an-inverse
The contents are as follows:
Apple documentation has a great example, suggest a situation where you might had problems by not having inverse rela Tionship. Lets map it into this case.
Lets assume you modeled it as follows
Note you are to-one relationship called "type", from Socialapp to Socialapptype. The relationship is non-optional and have a "deny" delete rule.
Now Lets consider following Code.
Socialapp *socialapp;socialapptype *apptype //assume entity instances correctly Instantiated [socialapp setsocialapptype:apptype< Span class= "pun" >; [managedobjectcontext deleteobject:< Span class= "PLN" >apptype; bool saved = [:&error
What we expect are to fail this context save since we has set the delete rule as Deny while relationship is non optional.
But here the save succeeds.
Reason is we haven ' t set a inverse relationship. Because of that Socialapp instance not get marked as changed when Apptype is deleted. So no validation happens for Socialapp before saving (It assumes no validation needed since no change happened). But actually a change happened. But it doesn ' t get reflected.
If we recall apptype by
SocialAppType *appType = [socialApp socialAppType];
Apptype is nil.
So weird isn ' t it. Get nil for non optional attribute?
So is in no trouble if you had set up the inverse relationship. Otherwise you are validation by writing the code as follows.
Socialapp *Socialapp;Socialapptype *Apptype;//assume entity instances correctly Instantiated [socialapp setsocialapptype:apptype< Span class= "pun" >; [managedobjectcontext deleteobject:< Span class= "PLN" >apptype; [socialapp setvalue: Nil Forkey:@ "Socialapptype" ]bool saved Span class= "pun" >= [managedobjectcontext Save :&error];
Inverse in IOS coredata relationship