【发布时间】:2012-09-24 05:45:33
【问题描述】:
我有两个实体。 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];
Fetch Request 是
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];
}
但在这里我从来没有得到结果。
【问题讨论】:
-
您能解释一下您要检索的内容吗?谢谢。
-
我想用谓词条件从第一个实体和第二个实体获取名称、部门和位置。
标签: iphone ios cocoa-touch cocoa core-data