我有一个与 CoreData 相关的问题,
我有这个.xcdatamodeld文件,它有2个实体, RCHTask 和 RCHTaskType
RCHTask与RCHTaskType
有多对一关系RCHTaskType与RCHTask
具有一对多关系我试图获取具有特定taskType的RCHTask实例 但我失败了,我试过这个:
- (NSArray *)returnTaskOfType:(NSString *)type
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [NSEntityDescription entityForName:@"RCHTask"
inManagedObjectContext:self.context];
request.entity = e;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"taskType == %@", type];
request.predicate = predicate;
NSError *error = nil;
NSArray *result = [self.context executeFetchRequest:request
error:&error];
return result;
}
但不工作你能告诉我该怎么做吗?
答案 0 :(得分:2)
您必须使用关系的名称。然后修改代码以获取任务,并使用关系名称指定谓词:
- (NSArray *)returnTaskOfType:(NSString *)type
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [NSEntityDescription entityForName:@"RCHTask"
inManagedObjectContext:self.context];
request.entity = e;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"relationship.taskType == %@", type];
request.predicate = predicate;
NSError *error = nil;
NSArray *result = [self.context executeFetchRequest:request
error:&error];
return result;
}