我有两个实体。 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 :(得分: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
和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);
}
答案 1 :(得分:0)
您需要设置反向关系。 你的Department对象需要看起来像这样:
@interface Department : NSManagedObject
@property (nonatomic, retain) NSString * location;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *deptEmp1;
就像所有Departement都有一个员工和员工在一个部门的工作。
在您的谓词上,您需要将“deptEmp1.name”调整为“deptEmp.name”