在ios 中的NSRelationshipDescription中的删除规则一共有四类如下:
//NSNoActionDeleteRule,
无任何删除动作。
如:A表中的关系字段值与B表中的键值相对应,当删除B表中的数据时,A中的关系字段值保持不变。
//NSNullifyDeleteRule
空删除规则。
如:A表中的关系字段值与B表中的键值相对应,当删除B表中的数据时,A中的关系字段值变为了NULL。
//NSCascadeDeleteRule,
联动删除规则。
如:当把A表中的关系设置为联动删除时,哪么A表在删除数据时候,将对联动表的数据也进行删除。
//NSDenyDeleteRule
依赖删除规则。
//如删除部门时必须确保该部门员工表中的数据为空或转到别处,否则删除失败。
1对1 单向删除关系。
静态设置。
在Car 表中添加一个关系取名为details,描述关联为CarStyle反向关系名为main,同时将删除规则设置为cascade
在CarStyle表中添加一个关系名为main描述关联为Car反向关系名为details.
动态设置。
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:self.managedObjectContext];
NSDictionary *rlation = [entity relationshipsByName];
NSRelationshipDescription *relation = [rlation objectForKey:@"details"];对Car表中的details关系进行设置规则
[relation setDeleteRule:NSCascadeDeleteRule];
[self.managedObjectContext save:nil];
效果:
当删除Car中的数据时 CarStyle相关联的数据也被删除。但在删除CarStyle时不删除Car中的数据,只是将关系字段设置为NULL。
1对1双向删除关系。
即A表,B表中的关系字段的删除规则都同时设为NSCascadeDeleteRule
静态设置
将Car和CarStyle的关系字段的删除规则都设置为Cascade
动态设置
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car"inManagedObjectContext:self.managedObjectContext];
NSDictionary *rlation = [entity relationshipsByName];
NSRelationshipDescription *relation = [rlation objectForKey:@"details"];
[relation setDeleteRule:NSCascadeDeleteRule];
entity = [NSEntityDescription entityForName:@"CarStyle" inManagedObjectContext:self.managedObjectContext];
rlation = [entity relationshipsByName];
relation = [rlation objectForKey:@"main"];
[relation setDeleteRule:NSCascadeDeleteRule];
[self.managedObjectContext save:nil];
1对N删除关系。
在一对N的关系中,需要在MODEL文件里设置to - many relationship 打上勾。
如图设置:
当一对多关系想在删除主表记录里,同时删除子表中对应主表数据的所有子表记录。这里需要设置主表的关系字段的删除规则为NSCascadeDeleteRule
动态设置,只能设置删除规则,对于一对多关系不能动态代码控制,只能静态设置好,因为要通过MODEL文件生成相应的关系表。
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:self.managedObjectContext];
NSDictionary *rlation = [entity relationshipsByName];
NSRelationshipDescription *relation = [rlation objectForKey:@"dept"];
[relation setDeleteRule:NSCascadeDeleteRule];
1对N依赖删除关系。
静态设置
插入测试数据。
- (void)insert1V1Data
{
//在Car表中添加两条数据
NSManagedObject * car1 = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:self.managedObjectContext];
[car1 setValue:@"1" forKey:@"name"];
[car1 setValue:@"1" forKey:@"engine"];
[car1 setValue:@"1" forKey:@"wheel"];
NSManagedObject * car2 = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:self.managedObjectContext];
[car2 setValue:@"2" forKey:@"name"];
[car2 setValue:@"2" forKey:@"engine"];
[car2 setValue:@"2" forKey:@"wheel"];
NSManagedObject * carstyle1 = [NSEntityDescription insertNewObjectForEntityForName:@"CarStyle" inManagedObjectContext:self.managedObjectContext];
[carstyle1 setValue:@"3" forKey:@"color"];
[carstyle1 setValue:[NSNumber numberWithInt:3] forKey:@"width"];
[carstyle1 setValue:[NSNumber numberWithInt:3] forKey:@"height"];
NSManagedObject * carstyle2 = [NSEntityDescription insertNewObjectForEntityForName:@"CarStyle" inManagedObjectContext:self.managedObjectContext];
[carstyle2 setValue:@"4" forKey:@"color"];
[carstyle2 setValue:[NSNumber numberWithInt:4] forKey:@"width"];
[carstyle2 setValue:[NSNumber numberWithInt:4] forKey:@"height"];
[car1 setValue:carstyle1 forKey:@"details"];
[carstyle1 setValue:car1 forKey:@"main"];
[car2 setValue:carstyle2 forKey:@"details"];
[carstyle2 setValue:car2 forKey:@"main"];
[self.managedObjectContext save:nil];
}
- (void)delete1V1Data:(NSInteger)table
{
if (table==1)
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:context];
NSFetchRequest *cmd = [[[NSFetchRequest alloc]init]autorelease];
[cmd setEntity:entity];
//设置查询条件
NSPredicate *condition = [NSPredicate predicateWithFormat:@"name = %@",@"2"];
[cmd setPredicate:condition];
NSArray *records = [context executeFetchRequest:cmd error:nil];
if ([records count]>0) {
NSManagedObject *rec = [records objectAtIndex:0];
[context deleteObject:rec];
[context save:nil];
}
}
else
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarStyle" inManagedObjectContext:context];
NSFetchRequest *cmd = [[[NSFetchRequest alloc]init]autorelease];
[cmd setEntity:entity];
//设置查询条件
NSPredicate *condition = [NSPredicate predicateWithFormat:@"color = %@",@"3"];
[cmd setPredicate:condition];
NSArray *records = [context executeFetchRequest:cmd error:nil];
if ([records count]>0) {
NSManagedObject *rec = [records objectAtIndex:0];
[context deleteObject:rec];
[context save:nil];
}
}
}
- (void)insert1VNData
{
NSManagedObject *main = [NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:self.managedObjectContext];
[main setValue:@"OK" forKey:@"name"];
NSManagedObject *dpt1 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
[dpt1 setValue:@"A" forKey:@"deptName"];
NSManagedObject *dpt2 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
[dpt2 setValue:@"B" forKey:@"deptName"];
[dpt1 setValue:main forKey:@"cmpy"];
[dpt2 setValue:main forKey:@"cmpy"];
[self.managedObjectContext save:nil];
}
- (void)delete1VNofNData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Department" inManagedObjectContext:context];
NSFetchRequest *cmd = [[[NSFetchRequest alloc]init]autorelease];
[cmd setEntity:entity];
//设置查询条件
NSPredicate *condition = [NSPredicate predicateWithFormat:@"deptName = %@",@"A"];
[cmd setPredicate:condition];
NSArray *records = [context executeFetchRequest:cmd error:nil];
if ([records count]>0) {
NSManagedObject *rec = [records objectAtIndex:0];
[context deleteObject:rec];
[context save:nil];
}
}
- (void)delete1VNof1Data
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:context];
NSFetchRequest *cmd = [[[NSFetchRequest alloc]init]autorelease];
[cmd setEntity:entity];
//设置查询条件
NSPredicate *condition = [NSPredicate predicateWithFormat:@"name = %@",@"OK"];
[cmd setPredicate:condition];
NSArray *records = [context executeFetchRequest:cmd error:nil];
if ([records count]>0) {
NSManagedObject *rec = [records objectAtIndex:0];
[context deleteObject:rec];
NSError* err;
[context save:&err];
if (!err)
{
NSLog(@"%@",[err localizedDescription]);
}
}
}