Two-way link operation for getting started with IOS data management tool CoreData

Source: Internet
Author: User

IOSData management toolsCoreDataGetting startedBidirectional relationshipThe operation is the content to be introduced in this article.CoreDataGetting startedBidirectional relationshipFor more information, see the details.

In two-way mode, only one side is specified, and the other side is automatically specified. For example, if an account has only one contact and one contact corresponds to one account, the account table and contact table have a one-to-one relationship, and they are specified as two-way, for example, cnt1.act = act1; cnt2.act = cnt2; act1.cnt and act2.cnt are also values. The two comments are optional.

Delete rule

Here, the cnt deletion rule in the Account table is set to cascade, so cnt1 is also deleted when act1 is deleted. The Contact table does not set the delete rule of act under it as cascade. When cnt1 is deleted, act1 still exists.

One-to-many relationship

It is unreasonable to assume that there is only one contact in an account. It is just a hypothesis that there are many contacts under an account. This is a one-to-many relationship, the above two rules also apply to this relationship. That is to say, the cnt under the Account is set to cascade. When an account is deleted, all its contacts are also deleted, the act in the contact table is set to nullify. When a contact is deleted, the account table does not change.

 
 
  1.  Account *act1 = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:self.managedObjectContext];   
  2.   act1.passport = @"passport1";   
  3.   act1.password = @"password1";   
  4.   Account *act2 = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:self.managedObjectContext];   
  5.   act2.passport = @"passport2";   
  6.   act2.password = @"password2";   
  7.    Contact *cnt1 = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];   
  8.  cnt1.passport = @"passport1";  
  9.   cnt1.nickname = @"nickname1";  
  10.  cnt1.act = act1;  
  11.  Contact *cnt2 = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];  
  12.    cnt2.passport = @"passport2";  
  13.   cnt2.nickname = @"nickname2";  
  14.   cnt2.act = act2;  
  15. //    act1.cnt = cnt1;  
  16. //    act2.cnt = cnt2;      
  17.   [self.managedObjectContext save:nil];  
  18.   // Delete a Account  
  19.     NSFetchRequest *fr = [[NSFetchRequest alloc] init];  
  20.   NSEntityDescription *ed = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:self.managedObjectContext];  
  21.   [fr setEntity:ed];  
  22.        NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];  
  23.    for (Account *act in ary) {  
  24.       //NSLog(@"%@  %@  %@  %@", act.passport, act.password, act.cnt.passport, act.cnt.nickname);  
  25.          if ([act.passport isEqualToString:@"passport2"]) {  
  26.            NSLog(@"DEL passport2");  
  27.            [self.managedObjectContext deleteObject:act];  
  28.        }  
  29.    }  
  30.   [self.managedObjectContext save:nil];  
  31.   // Delete a Contact  
  32.    NSFetchRequest *fr = [[NSFetchRequest alloc] init];  
  33.    NSEntityDescription *ed = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];  
  34.   [fr setEntity:ed];  
  35.    NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];  
  36.   NSLog(@"%d", ary.count);  
  37.    for (Contact *cnt in ary) {  
  38.        //NSLog(@"%@  %@  %@  %@", act.passport, act.password, act.cnt.passport, act.cnt.nickname);  
  39.        if ([cnt.nickname isEqualToString:@"nickname1"]) {  
  40.            NSLog(@"DEL nickname1");  
  41.            [self.managedObjectContext deleteObject:cnt];  
  42.        }  
  43.    }  
  44.      
  45.   [self.managedObjectContext save:nil];  
  46.   // From Account62     NSFetchRequest *fr = [[NSFetchRequest alloc] init];  
  47.    NSEntityDescription *ed = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:self.managedObjectContext];  
  48.    [fr setEntity:ed];  
  49.    NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];  
  50.    for (Account *act in ary) {  
  51.        NSLog(@"%@  %@  %@  %@", act.passport, act.password, act.cnt.passport, act.cnt.nickname);  
  52.    }  
  53.    // From Contact73     NSFetchRequest *fr = [[NSFetchRequest alloc] init];  
  54.    NSEntityDescription *ed = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];  
  55.    [fr setEntity:ed];  
  56.    NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];  
  57.    for (Contact *cnt in ary) {  
  58.        NSLog(@"%@  %@  %@  %@", cnt.passport, cnt.nickname, cnt.act.passport, cnt.act.password);  

Summary:IOSData management toolsCoreDataGetting startedBidirectional relationshipThe operation is complete. I hope this article will help you!

Related Article

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.