核心数据:从多个实体或关系获取结果 [英] Core Data: Fetch result from multiple entities or relationship
问题描述
我有两个实体。 员工
实体
@interface员工:NSManagedObject
@property(nonatomic,retain)NSString * dept;
@property(nonatomic,retain)NSString * email;
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)Department * deptEmp;
@end
和部门
实体
@interface部门:NSManagedObject
@property(nonatomic,retain)NSString * 位置;
@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:@EmployeeinManagedObjectContext: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];
}
但这里我从来没有结果。
<我不知道你想要实现什么,但如果你想检索一个
Employee
谁工作具体部门名称和在特定位置,我将使用以下代码: NSMutableString * queryString = [NSMutableString stringWithFormat :@deptEmp1.name ==%@ AND deptEmp1.location ==%@,@Apple,@Cupertino];
NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@EmployeeinManagedObjectContext: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);
}
少量注释
为什么对请求使用锁定?
你设置了一个inverse rel?
也许你需要在部门
和员工
之间建立一对多的关系? / p>
尝试让我知道。
编辑 b
尝试这个。我没有注意到你的问题中的查询字符串。
NSPredicate * predicate = [NSPredicate predicateWithFormat:@deptEmp1.name = =%@ AND deptEmp1.location ==%@,@Apple,@Cupertino];
NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@EmployeeinManagedObjectContext: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 b
NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@EmployeeinManagedObjectContext: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);
}
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
and Department
entity
@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];
And Fetch Request is
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
[request setResultType:NSDictionaryResultType]; // NSFetchRequestResultType - NSDictionaryResultType
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]];
[request setIncludesSubentities:YES];
Setting Predicate
if(![queryString isEqualToString:@""])
{
[request setPredicate:[NSPredicate predicateWithFormat:queryString]];
}
NSError *error = nil;
NSArray *returnArray = nil;
Fetching Result
@synchronized(self)
{
returnArray = [self.managedObjectContext executeFetchRequest:request error:&error];
}
But here I never get result.
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);
}
Few notes
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 and let me know.
Edit
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);
}
Edit 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屋!