这是一个后续的问题我刚才的问题上建立一个NSPredicate:核心数据 - 从多对多关系
Core data: Managing employee contracts in a many-to-many relationship?
有关于这一问题的示意图,并作为快速提醒存在以下:
company --<contracts>-- employees
我已经能够手动保存内部的各个实体的1个实体,并证实他们都在NSLog的。
我创建了一个列出所有公司的CompanyListPage。这个想法是,当你点击一家公司时,你将看到一份所有与该公司签订合同的员工名单。
作为上下文请看下图:
Company:
name: A
Contract:
length: 33 wks
salary: 20000
Employee:
name: Bob Jones
在我didSelectRowAtIndex页我CompanyListPage内我有以下。
- (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;
}
显然,这是错误的,因为它不是:
一)展望合同实体 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抓住所有的合同数据,而不仅仅是合同数据;不过我可能会误解。
再次,对此的帮助将不胜感激。
我会给它一个去。谢谢。 – zardon 2011-03-24 11:10:02
它似乎在工作,我得到了一份在我的tableViewController中为公司工作的员工名单。但是我收到合同信息时遇到了困难。我修正了我原来的问题,确切的问题。 – zardon 2011-03-24 12:47:25