IOS coredata Relationship in inverse

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.