iPhone - 获取核心数据的数据

时间:2010-12-21 19:44:12

标签: iphone core-data ios

我有一个这样设计的数据结构:

employee
. department
. building
. sector
> number (this is a relationship to employeeData)



employeeData
. name
. position
. salary
> number (this is a relationship to employee)

这样,每个员工只有一个employeeData条目。

我有所有实体的课程。

现在,我如何获取匹配特定号码的每个员工,然后获取与员工相对应的employeeData?

我需要的是这个

“找到employee.number = X的employeeData”

谢谢

1 个答案:

答案 0 :(得分:3)

这样的事情:

NSManagedObjectContext * context  = [[NSApp delegate] managedObjectContext];
NSManagedObjectModel   * model    = [[NSApp delegate] managedObjectModel];
NSDictionary           * entities = [model entitiesByName];
NSEntityDescription    * entity   = [entities valueForKey:@"Employee"];

NSPredicate * predicate;
predicate = [NSPredicate predicateWithFormat:@"number = %@", number];


NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
[fetch setEntity: entity];
[fetch setPredicate: predicate];


NSArray * results = [context executeFetchRequest:fetch error:nil];
[fetch release];

Employee *emp = [results objectAtIndex:0];
EmployeeData *data = [emp data];

请记住,如果在核心数据模型中定义了关系,则会自动获取数据关系。对其进行了提取和修改,以适应以下问题:http://www.cocoadevcentral.com/articles/000086.php