2012-10-03 7 views
5

私は2つのエンティティを持っています。私は、次の述語コアデータ:複数のエンティティまたは関係からの結果を取得します。

NSMutableString *queryString = [NSMutableString stringWithFormat:@"(name = 'Apple') AND (deptEmp1.location like 'Cupertino')"]; 

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext]; 

との両方から情報を取得し、要求を取得しようとしています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; 

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

に「deptEmp1.name」を調整する必要がありますか?ありがとう。 –

+0

私は最初のエンティティと位置から2番目の述語条件でname、deptをフェッチします。 – 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の間に一対多の関係を設定する必要がありますか?

私に教えてください。

編集

、このいずれかを試してみてください。私はあなたの質問のクエリ文字列に気付かなかった。

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; 

全てのデパートが1つのデパートの従業員のコレクションと従業員の仕事を持っています。

そして、あなたの述語にあなたはあなたが取得したいのか説明してもらえ「deptEmp.name」

関連する問題