我有兩個實體。 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];
}
但在這裏我從來沒有得到結果。
你能解釋一下你想要檢索什麼?謝謝。 –
我想從第一個實體和第二個位置獲取名稱,部門名稱和謂詞條件。 – kamleshwar