5

私は2つのエンティティを持っています。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];
}

しかし、ここでは結果が得られません。

4

2 に答える 2

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);
}

いくつかのメモ

なぜリクエストにロックを使用するのですか?逆関係を設定しましたか?Departmentたぶん、との間に1対多の関係を設定する必要があり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);
}
于 2012-10-03T10:22:27.587 に答える