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