CoreData相关问题?

时间:2015-07-27 22:50:29

标签: ios objective-c

我有一个与 CoreData 相关的问题,

我有这个.xcdatamodeld文件,它有2个实体, RCHTask RCHTaskType

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

但不工作你能告诉我该怎么做吗?

1 个答案:

答案 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;
}