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.
- Account *act1 = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:self.managedObjectContext];
- act1.passport = @"passport1";
- act1.password = @"password1";
- Account *act2 = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:self.managedObjectContext];
- act2.passport = @"passport2";
- act2.password = @"password2";
- Contact *cnt1 = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
- cnt1.passport = @"passport1";
- cnt1.nickname = @"nickname1";
- cnt1.act = act1;
- Contact *cnt2 = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
- cnt2.passport = @"passport2";
- cnt2.nickname = @"nickname2";
- cnt2.act = act2;
- // act1.cnt = cnt1;
- // act2.cnt = cnt2;
- [self.managedObjectContext save:nil];
- // Delete a Account
- NSFetchRequest *fr = [[NSFetchRequest alloc] init];
- NSEntityDescription *ed = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:self.managedObjectContext];
- [fr setEntity:ed];
- NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];
- for (Account *act in ary) {
- //NSLog(@"%@ %@ %@ %@", act.passport, act.password, act.cnt.passport, act.cnt.nickname);
- if ([act.passport isEqualToString:@"passport2"]) {
- NSLog(@"DEL passport2");
- [self.managedObjectContext deleteObject:act];
- }
- }
- [self.managedObjectContext save:nil];
- // Delete a Contact
- NSFetchRequest *fr = [[NSFetchRequest alloc] init];
- NSEntityDescription *ed = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
- [fr setEntity:ed];
- NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];
- NSLog(@"%d", ary.count);
- for (Contact *cnt in ary) {
- //NSLog(@"%@ %@ %@ %@", act.passport, act.password, act.cnt.passport, act.cnt.nickname);
- if ([cnt.nickname isEqualToString:@"nickname1"]) {
- NSLog(@"DEL nickname1");
- [self.managedObjectContext deleteObject:cnt];
- }
- }
-
- [self.managedObjectContext save:nil];
- // From Account62 NSFetchRequest *fr = [[NSFetchRequest alloc] init];
- NSEntityDescription *ed = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:self.managedObjectContext];
- [fr setEntity:ed];
- NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];
- for (Account *act in ary) {
- NSLog(@"%@ %@ %@ %@", act.passport, act.password, act.cnt.passport, act.cnt.nickname);
- }
- // From Contact73 NSFetchRequest *fr = [[NSFetchRequest alloc] init];
- NSEntityDescription *ed = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
- [fr setEntity:ed];
- NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];
- for (Contact *cnt in ary) {
- 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!