核心数据:从多个实体或关系获取结果
我有两个实体。 Employee
实体
@interface Employee : NSManagedObject @property (nonatomic, retain) NSString * dept; @property (nonatomic, retain) NSString * email; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) Department *deptEmp; @end
和Department
实体
@interface Department : NSManagedObject @property (nonatomic, retain) NSString * location; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) Employee *deptEmp1;
我试图从下面的谓词中获取信息
NSMutableString *queryString = [NSMutableString stringWithFormat:@"(name = 'Apple') AND (deptEmp1.location like 'Cupertino')"]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
并提取请求是
NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; [request setResultType:NSDictionaryResultType]; // NSFetchRequestResultType - NSDictionaryResultType [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; [request setIncludesSubentities:YES];
设置谓词
if(![queryString isEqualToString:@""]) { [request setPredicate:[NSPredicate predicateWithFormat:queryString]]; } NSError *error = nil; NSArray *returnArray = nil;
获取结果
@synchronized(self) { returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; }
但是在这里我从来没有得到结果
我不确定你想达到什么目的,但是如果你想要检索一个在特定部门名称和特定位置工作的Employee
,我将使用下面的代码:
NSMutableString *queryString = [NSMutableString stringWithFormat:@"deptEmp1.name == %@ AND deptEmp1.location == %@", @"Apple", @"Cupertino"]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; [request setIncludesSubentities:YES]; NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; if([returnArray count] > 0) { Employee* emp = [returnArray objectAtIndex:0]; NSLog(@"%@ %@ %@", emp.name, emp.dept, emp.deptEmp.location); }
几个笔记
你为什么要在请求上使用锁? 你有没有设置一个反相对? 也许你需要在Department
和Employee
之间build立一个一对多的关系吗?
试着让我知道。
编辑
试试这个。 我没有注意到你的问题中的查询string。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deptEmp1.name == %@ AND deptEmp1.location == %@", @"Apple", @"Cupertino"]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; [request setPredicate:predicate]; [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; [request setIncludesSubentities:YES]; NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; if([returnArray count] > 0) { Employee* emp = [returnArray objectAtIndex:0]; NSLog(@"%@ %@ %@", emp.name, emp.dept, emp.deptEmp.location); }
编辑2
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]]; NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error]; for(Employee* emp in returnArray) { NSLog(@"%@", emp); NSLog(@"%@", emp.deptEmp); }
你需要设置一个反向关系。 您的部门对象需要如下所示:
@interface Department : NSManagedObject @property (nonatomic, retain) NSString * location; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSSet *deptEmp1;
就像所有的部门都有一个员工和一个员工在一个部门工作。
在你的谓词上,你需要调整“deptEmp1.name”为“deptEmp.name”