表关联
比如企业下有众多部门,而员工属于某一个部门,则在员工表与部门表之间产生了关联。
在上一篇文章中已经有一张“Person”表了,现在创建一个“国家”表“Country”
添加关联
然后把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 =