开发者

iPhone - fetching data on core data

开发者 https://www.devze.com 2023-02-01 03:55 出处:网络
I have a data structure designed like this: employee . department . building . sector > number (this is a relationship to employeeData)

I have a data structure designed like this:

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



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

this way开发者_如何学Go, there will be only one employeeData entry per employee.

I have classes for all entities.

Now, how do I fetch every employee that matches a particular number and then the employeeData that corresponds to the employee?

what I need is this

"find employeeData for employee.number = X"

thanks


Something like this:

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];

Remember that the data relationship is automatically fetched due if the relationship is defined in the core data model. This was extracted and modified to fit the question from: http://www.cocoadevcentral.com/articles/000086.php

0

精彩评论

暂无评论...
验证码 换一张
取 消