IOS CoreData 多表查询demo解析(二)--插入测试数据
1、查询 HR 部门的张三
//查询 开发部门中张三的工资等级
NSLog(@"----------查询 开发部门中张三的工资等级----------");
NSLog(@"----------第1种方法----------");
//第1种方法
NSEntityDescription * emEty = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *frq = [[NSFetchRequest alloc]init];
[frq setEntity:emEty];
NSPredicate * cdt = [NSPredicate predicateWithFormat:@"em_name = %@", @"张三"];
[frq setPredicate:cdt];
NSArray *objs =[self.managedObjectContext executeFetchRequest:frq error:nil];
NSPredicate * filter = [NSPredicate predicateWithFormat:@"dept.dp_deptname = %@",@"HR"];
NSArray * ret = [objs filteredArrayUsingPredicate:filter];
Employee* r = (Employee*)[ret lastObject];
NSLog(@"%@", r.em_name);
NSLog(@"%d", r.em_age);
NSLog(@"%d", r.em_sex);
NSLog(@"%@", r.em_bankcardid);
NSLog(@"%@", r.post.salary.sy_level);
NSLog(@"%f", r.post.salary.sy_scale);
NSLog(@"%@", r.bank.bk_name);
NSLog(@"%@", r.bank.bk_address);
//[self.managedObjectContext deleteObject:r];
//[self saveContext];
NSLog(@"----------第2种方法----------");
//第2种方法
NSEntityDescription * emEty1 = [NSEntityDescription entityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *frq1 = [[NSFetchRequest alloc]init];
[frq1 setEntity:emEty1];
NSPredicate * cdt1 = [NSPredicate predicateWithFormat:@"dp_deptname = %@", @"HR"];
[frq1 setPredicate:cdt1];
Department *objs1 =[[self.managedObjectContext executeFetchRequest:frq1 error:nil] lastObject];
NSSet * ret1 = objs1.employeeship;
NSPredicate * filter1 = [NSPredicate predicateWithFormat:@"em_name = %@",@"张三"];
Employee* r1 = [[ret1 filteredSetUsingPredicate:filter1] anyObject];
NSLog(@"%@", r1.em_name);
NSLog(@"%d", r1.em_age);
NSLog(@"%d", r1.em_sex);
NSLog(@"%@", r1.em_bankcardid);
NSLog(@"%@", r1.post.salary.sy_level);
NSLog(@"%f", r1.post.salary.sy_scale);
NSLog(@"%@", r1.bank.bk_name);
NSLog(@"%@", r1.bank.bk_address);
2、查询 运维部欧阳
//查询运维部名为欧阳的工资等级及开户银行
NSLog(@"----------查询运维部名为欧阳的工资等级及开户银行----------");
NSLog(@"----------第1种方法----------");
//第1种方法
NSEntityDescription * entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:entity];
NSPredicate * qcmd = [NSPredicate predicateWithFormat:@"em_name = %@ ",@"欧阳"];
[fetch setPredicate:qcmd];
NSArray * obs = [self.managedObjectContext executeFetchRequest:fetch error:nil];
NSPredicate * filter2 = [NSPredicate predicateWithFormat:@"dept.dp_deptname = %@",@"DEV"];
NSArray * ret2 = [obs filteredArrayUsingPredicate:filter2]; //从数组中进行过滤。
Employee* r2 = [ret2 lastObject];
NSLog(@"%@", r2.em_name);
NSLog(@"%d", r2.em_age);
NSLog(@"%d", r2.em_sex);
NSLog(@"%@", r2.em_bankcardid);
NSLog(@"%@", r2.post.salary.sy_level);
NSLog(@"%f", r2.post.salary.sy_scale);
NSLog(@"%@", r2.bank.bk_name);
NSLog(@"%@", r2.bank.bk_address);
NSLog(@"----------第2种方法----------");
//第2种方法
NSEntityDescription * emEty3 = [NSEntityDescription entityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *frq3 = [[NSFetchRequest alloc]init];
[frq3 setEntity:emEty3];
NSPredicate * cdt3 = [NSPredicate predicateWithFormat:@"dp_deptname = %@", @"DEV"];
[frq3 setPredicate:cdt3];
Department *objs3 =[[self.managedObjectContext executeFetchRequest:frq3 error:nil] lastObject];
NSSet * ret3 = objs3.employeeship;
NSPredicate * filter3 = [NSPredicate predicateWithFormat:@"em_name = %@",@"欧阳"];
Employee* r3 = [[ret3 filteredSetUsingPredicate:filter3] anyObject];
NSLog(@"%@", r3.em_name);
NSLog(@"%d", r3.em_age);
NSLog(@"%d", r3.em_sex);
NSLog(@"%@", r3.em_bankcardid);
NSLog(@"%@", r3.post.salary.sy_level);
NSLog(@"%f", r3.post.salary.sy_scale);
NSLog(@"%@", r3.bank.bk_name);
NSLog(@"%@", r3.bank.bk_address);
多表查询主要就在于表之间建立好相关的关联关系(relationship),其次就是充分的使用NSPredicate这个查询条件来进行过滤。
联系客服