【问题标题】:Core Data: Fetch result from multiple entities or relationship核心数据:从多个实体或关系中获取结果
【发布时间】: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


【解决方案1】:

我不确定您想要实现什么,但如果您想检索在特定部门名称和特定位置工作的 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);
}

几点说明

为什么对请求使用锁定? 你设置了反向rel 吗? 也许您需要在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);
}

【讨论】:

  • 感谢您的回复,在查询字符串中,我已根据您的说明将 deptEmp1 更改为 deptEmp 和设置,它运行没有任何错误。但什么也不返回,即使我在数据库中有相同的记录。
  • 对不起,亲爱的,但这不起作用。没有我可以遵循的示例代码吗?,因为我尝试了 2-3 天,但没有找到任何解决方案。我不确定问题出在代码还是模型中。感谢您的帮助
  • 我添加了一个编辑。你能看到你感兴趣的员工吗?
  • 您的“编辑 2”给出了完美的结果。现在看来有希望了。
  • @kamleshwar 打印部门。你看到了什么?见编辑。
【解决方案2】:

您需要设置反向关系。 您的 Department 对象需要如下所示:

@interface Department : NSManagedObject

@property (nonatomic, retain) NSString * location;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *deptEmp1;

就像所有部门都有一个员工集合和一个员工在一个部门工作。

在您的谓词上,您需要将“deptEmp1.name”调整为“deptEmp.name”

【讨论】:

    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多