0

这是一个后续的问题我刚才的问题上建立一个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抓住所有的合同数据,而不仅仅是合同数据;不过我可能会误解。

再次,对此的帮助将不胜感激。

回答

1

我认为,如果你使用ANY关键字,你会等式的左边恢复到一个单一的量,这将等式的右边同意:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@“ANY contracts.employer == %@“, employer]; 
+0

我会给它一个去。谢谢。 – zardon 2011-03-24 11:10:02

+0

它似乎在工作,我得到了一份在我的tableViewController中为公司工作的员工名单。但是我收到合同信息时遇到了困难。我修正了我原来的问题,确切的问题。 – zardon 2011-03-24 12:47:25

0

我想我有现在回答。

我在我的表格视图中做了以下操作,并获得了有效关系和所有输出;但是我不确定我是否正确,因为它涉及到for循环。

我使用以下与我的解决方案相关的StackOverflow question/answer

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    Employee *emp = [fetchedResultsController objectAtIndexPath:indexPath]; 

    NSString *firstname = emp.firstname; 
    NSString *surname = emp.surname; 

    NSString *fullname = [firstname stringByAppendingString:@" "]; 
    fullname   = [fullname stringByAppendingString:surname]; 

    NSLog(@"Employee: %@", fullname); 

    // Output the contract deals. 
    for (Contracts *deals in emp.contracts) 
    { 

     NSNumber *cLength = deals.length; 
     NSNumber *cSalary = deals.salary; 

     NSLog(@"Length: %@", cLength); 
     NSLog(@"Salary: %@", cSalary); 
    } // next 
} 

对我来说,下一步就是将其包含在自定义视图中,并使用自定义标签来保存所有这些信息。

如果for循环不是最有效的方法,我会欢迎任何建议/改进,甚至被认为是最佳实践。

谢谢。

0

看起来好像很顺利。我认为你需要从那里设置单元格的文本:cell.textLabel = fullname。

但我不清楚为什么你要重新回到合同关系 - 直到我看到顶部的示例文本。在那里,它看起来像你想要的是合同列表为雇主,并为每个合同你想要显示其对一个雇员关系和其他attrs。在这种情况下,你根本不需要新的获取;你已经拥有了雇主,并且通过其一对多的合同关系,你还拥有合同实体,并通过他们获得期限,薪水,雇员等等。

当在雇主表选择的变化,你会做这样的事情:

NSUInteger row = [indexPath row]; 
Employer *employer = [self.arrayOfEmployersUsedToPopulateTable objectAtIndex:row]; 
self.selectedEmployer = employer; 
self.arrayOfSelectedEmployersContracts = [employer.contracts allObjects]; // which probably faults them, but I think that’s OK here 
// Then make a call to reload the other table, the one presenting the contracts info. 

在合同表,你会重提到选定的雇主,和现在的信息对于每个合同:

​​

PS对于UITableCell的东西,我参考了Mark和LaMarche的“开始iPhone开发”,“使用新的表格视图单元”。您也可能喜欢这本书。

+0

糟糕,我一直在使用NSTableView,所以我正在考虑使用selectionDidChange委托方法来缓存雇主对象。您可能正在深入细节表视图。你会以某种方式从superview中检索雇主对象。对困惑感到抱歉。 – Wienke 2011-03-25 00:20:49

+0

感谢您的帮助。我很感激。我重新回到合同关系的原因是因为:a)我想把合同期限和合同工资退回来。我希望能够按整个长度对整个抓取进行排序(即:给我一份合同到期的人员列表)。我会看看这本书。我有几本核心数据手册,它的内容非常丰富。我会尽快详细探讨你的答案。 – zardon 2011-03-26 18:05:28