作者:0Hey0ne | 来源:互联网 | 2024-11-18 12:59
在企业中,通常会有多个部门,每个员工隶属于某个部门。这种情况下,员工表和部门表之间就会形成关联关系。本文将详细介绍如何在CoreData中实现表关联,并通过示例代码展示如何添加和查询关联数据。
表关联概述
在企业中,通常会有多个部门,每个员工隶属于某个部门。这种情况下,员工表和部门表之间就会形成关联关系。例如,在上一篇文章中我们已经创建了一张“Person”表,现在我们将创建一个“Country”表来演示表关联。
添加关联
首先,我们需要在CoreData模型中添加关联关系。假设我们已经有一个“Person”类,现在需要添加一个“Country”类,并建立两者之间的关联关系。
接下来,删除原有的“Person”类文件,重新生成新的“Person”类文件。重新生成后,你会发现“Person”类中多了一个关联属性。
添加关联数据
- (void)addPersonAndCountry {
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Country"];
NSPredicate *predicate = nil;
if (arc4random() % 2) {
predicate = [NSPredicate predicateWithFormat:@"name=%@", @"NaiCha"];
} else {
predicate = [NSPredicate predicateWithFormat:@"name=%@", @"KeKoCola"];
}
request.predicate = predicate;
NSError *error = nil;
NSArray *resultArray = [self.context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"查询失败,错误信息:%@", error);
} else {
if (resultArray.count == 1) {
Country *country = resultArray[0];
int numberID = arc4random() % 10000;
person.name = [NSString stringWithFormat:@"personSomeone%d", numberID];
int64_t persOnAge= arc4random() % 100;
person.age = personAge;
person.country = country;
[self.context save:&error];
if (error) {
NSLog(@"添加个人信息出错,错误:%@", error);
} else {
NSLog(@"添加个人信息:%@-%lld-%@", person.name, person.age, person.country.name);
}
} else {
NSLog(@"查询无此国籍,无法添加个人信息");
}
}
}
运行结果:
2022-03-04 21:19:48.855904+0800 CoreData实践1[77919:3270501] 添加个人信息:personSomeone6740-84-KeKoCola
通过上述代码,我们可以看到,不需要编写任何复杂的数据库操作代码,直接将关联的对象作为属性成员赋值即可完成关联,非常方便。
查找关联数据
- (void)findSomebodyByCountry:(NSString *)countryName {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"country.name=%@", countryName];
request.predicate = predicate;
NSError *error = nil;
NSArray *resultArray = [self.context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"查询失败,错误信息:%@", error);
} else {
NSLog(@"查询国籍为:%@的结果如下", countryName);
for (Person *person in resultArray) {
NSLog(@"name:%@ age:%lld country:%@", person.name, person.age, person.country.name);
}
}
}
运行结果:
2022-03-04 23:17:51.282876+0800 CoreData实践1[78883:3309210] 查询国籍为:NaiCha的结果如下
2022-03-04 23:17:51.283777+0800 CoreData实践1[78883:3309210] name:personSomeone8102 age:43 country:NaiCha
2022-03-04 23:17:51.283962+0800 CoreData实践1[78883:3309210] name:personSomeone5777 age:17 country:NaiCha
2022-03-04 23:17:51.284096+0800 CoreData实践1[78883:3309210] name:personSomeone966 age:17 country:NaiCha
2022-03-04 23:17:51.284221+0800 CoreData实践1[78883:3309210] name:personSomeone7477 age:57 country:NaiCha
2022-03-04 23:17:51.284329+0800 CoreData实践1[78883:3309210] name:personSomeone701 age:12 country:NaiCha
2022-03-04 23:17:51.284435+0800 CoreData实践1[78883:3309210] name:personSomeone2071 age:47 country:NaiCha
2022-03-04 23:17:51.284553+0800 CoreData实践1[78883:3309210] name:personSomeone1474 age:78 country:NaiCha
通过点语法增加约束条件,即可轻松实现关联数据的查询。