2012-10-03 91 views
5

我有两个实体。 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]; 
} 

但在这里我从来没有得到结果。

+0

你能解释一下你想要检索什么?谢谢。 –

+0

我想从第一个实体和第二个位置获取名称,部门名称和谓词条件。 – kamleshwar

回答

3

我不知道你想达到什么,但如果你想检索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); 
} 

几个笔记

为什么你对请求使用锁定? 你有没有设置一个反相对? 也许你需要在DepartmentEmployee之间建立一对多的rel?

试着让我知道。

编辑

试试这一个。我没有注意到你的问题中的查询字符串。

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); 
} 
+0

感谢您的回复,在查询字符串中,我已将deptEmp1更改为deptEmp并根据您的注释进行了设置,它运行时没有任何错误。但什么都不返回,即使我在DB中有相同的记录。 – kamleshwar

+1

@kamleshwar我添加了一个编辑。尝试一下。 –

+1

对不起,亲爱的,但这是行不通的。是否有任何示例代码,我可以遵循?,因为我试图从2-3天没有找到任何解决方案。我不确定问题出在代码或模型中。 感谢您的帮助 – kamleshwar

0

您需要设置一个反向的关系。 你处对象需要看起来像:

@interface Department : NSManagedObject 

@property (nonatomic, retain) NSString * location; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSSet *deptEmp1; 

像所有的部门已经Employee的集合,并在一个DEPARTEMENT一个员工的工作。

并在您的谓词你需要“deptEmp1.name”调整为“deptEmp.name”