我有一个与 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;
}
但是不工作,你能告诉我怎么做吗?
最佳答案
您必须使用关系的名称。然后修改您的代码以获取Tasks,并使用关系名称指定谓词:
- (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;
}