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

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

4

2 回答 2

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

几条笔记

为什么对请求使用锁定?你设置了反向rel 吗?Department也许你需要在and之间建立一个一对多的关系Employee

试着让我知道。

编辑

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

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);
}
于 2012-10-03T10:22:27.587 回答
0

您需要设置反向关系。您的 Department 对象需要如下所示:

@interface Department : NSManagedObject

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

就像所有部门都有一个员工集合和一个员工在一个部门工作。

在您的谓词上,您需要将“deptEmp1.name”调整为“deptEmp.name”

于 2013-11-26T13:24:41.430 回答