這是一個後續的問題我剛纔的問題上建立一個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