IOS数据管理工具CoreData入门之双向关系操作是本文要介绍的内容,主要来介绍CoreData入门双向关系的操作和学习,不多说,我们先来看详细内容介绍。
双向的时候,只用指定一边,另外一边自动指定,比如,如果一个帐号只有一个联系人,而一个联系人对应一个帐号的话,那么帐号表和联系人表就是一一对应的关系,将他们指定为双向的,如下面将cnt1.act = act1; cnt2.act = cnt2;则act1.cnt和act2.cnt也是有值的。注释掉的两句可有可无。
删除规则
这里将Account表下面的cnt的删除规则设定为cascade,则在删除act1的时候,cnt1也被删除了。而Contact表没有将它下面的act的删除规则设置为cascade,在删除cnt1的时候,act1还存在。
一对多的关系
上面假设一个帐号只有一个联系人是不合理的,那只是一种假设,一般都是一个帐号下面有很多联系人,这就是一对多的关系,上面的两个法则也适用于这种关系,也就是说帐号下面的cnt设置为cascade,删除一个帐号时,它所有的联系人也被删除了,而联系人表下面的act设置为nullify,当删除一个联系人的时候帐号表没有变化。
- 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);
小结:IOS数据管理工具CoreData入门之双向关系操作的内容介绍完了,希望本文对你有所帮助!