NSPredicate与关系

时间:2013-08-11 13:07:22

标签: cocoa core-data nspredicate

我正在尝试过滤显示Employees的表列表的结果。一个过滤器应显示属于某个公司的每个员工(基于公司名称)。

在Core Data中,实体公司与实体Employee具有To-Many关系。

_fetchRequest = [[NSFetchRequest alloc] init];
[_fetchRequest setEntity:[NSEntityDescription entityForName:@"Employee" inManagedObjectContext:_moc]];

NSPredicate *employeeNamePredicate = [NSPredicate predicateWithFormat:@"name != %@",@"Bob"];
NSPredicate *employeeCompanyNamePredicate = [NSPredicate predicateWithFormat:@"company.name == %@",companyName];

NSArray *filters = @[employeeNamePredicate, employeeCompanyNamePredicate];

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:filters];

[_fetchRequest setPredicate:compoundPredicate];

NSError *error = nil;
_filteredResult = [_moc executeFetchRequest:_fetchRequest error:&error];

但没有真正发生。 @“company.name”似乎没有按预期工作。我究竟做错了什么?

0 个答案:

没有答案