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這個查詢條件來進行過濾。