2012-10-03 96 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」