这是我之前的问题的后续问题:
关于这个问题有一个图表,作为快速提醒,有以下内容:
company --< contracts >-- employees
我已经能够在每个实体中手动保存 1 个实体,并在 NSLog 中验证它们。
我创建了一个列出所有公司的 CompanyListPage。这个想法是,当您单击一家公司时,您将看到与该公司签订合同的所有员工的列表。
如上下文见下文:
Company:
name: A
Contract:
length: 33 wks
salary: 20000
Employee:
name: Bob Jones
在我的 CompanyListPage 中的 didSelectRowAtIndex 页面中,我有以下内容。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Company *c = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"You clicked %@", c.name);
NSString *companyName = c.name;
EmployeesListPage *employeesListPage = [[EmployeesListPage alloc] initWithNibName:@"EmployeesListPage" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:employeesListPage animated:YES];
employeesListPage.title = companyName;
employeesListPage.managedObjectContext = self.context;
employeesListPage.managedObject = c;
[superstarsList release];
}
然而问题是,当我最终访问employeesListPage 时,我不确定我的NSPredicate 应该是什么样子。
目前,它是这样的:
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create and configure a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employees" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array
NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management.
[aFetchedResultsController release];
[fetchRequest release];
[authorDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
显然这是错误的,因为它不是:
a) 查看合同实体 b) 以任何方式、形式或形式使用公司实体
我知道我需要使用 NSPredicate,但我只知道如何让它说“找到合同长度 > 0 并与 A 公司合作的所有员工”,然后按人的姓名降序排列,甚至排序首先是最短的合同长度。
对此的任何指示或帮助都会很棒。谢谢你。
编辑:第一次尝试(删除,因为我按照下面提供的答案让它工作)
编辑:无法取回合同信息?
我已经能够让为 A 公司工作的所有员工重新回到我的表格视图控制器中。
但是,我想在我的单元格中显示有关员工及其合同期限/薪水的信息。
我尝试了以下方法:
Employee *emp = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *firstname = emp.firstname;
NSString *surname = emp.surname;
NSString *fullname = [firstname stringByAppendingString:@" "];
fullname = [fullname stringByAppendingString:surname];
// Logging tests
NSLog(@"Name: %@", fullname); // This is fine
NSLog(@"Contracts: %@", emp.empContracts); // This tells me of a problem with "Relationship fault for <NSRelationshipDescription: 0x602fdd0>"
我相信我需要让 NSPredicate 来获取所有的合约数据,而不仅仅是合约数据;但是我可能弄错了。
再次,非常感谢您对此的帮助。