IOS CoreData 多表查詢demo解析(三)--查詢

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.