核心数据:从多个实体或关系中获取结果 [英] Core Data: Fetch result from multiple entities or relationship

查看:21
本文介绍了核心数据:从多个实体或关系中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体.Employee 实体

I have two entities. Employee entity

@interface Employee : NSManagedObject

@property (nonatomic, retain) NSString * dept;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Department *deptEmp;

@end

部门实体

@interface Department : NSManagedObject

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

我正在尝试使用以下谓词从两者中获取信息

I am trying to fetch information from both with following predicate

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

但在这里我从来没有得到结果.

But here I never get result.

推荐答案

我不确定您想要实现什么,但是如果您想检索在特定部门名称和工作的 Employee在特定位置,我将使用以下代码:

I'm not sure what you want to achieve but if you want to retrieve an Employee who works in a specific department name and in a specific location, I'll use a the following code:

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 之间建立一对多的关系?

Why do you use a lock on the request? Have you set a inverse rel? Maybe do you need to set up a one-to-many rel between Department and Employee?

试着告诉我.

编辑

试试这个.我没有注意到你问题中的查询字符串.

Try this one. I didn't notice the query string in your question.

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

这篇关于核心数据:从多个实体或关系中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆