これは、私の以前の質問に対するフォローアップの質問です。
コア データ: 多対多の関係で従業員契約を管理していますか?
その質問には図があり、簡単なリマインダーとして次のとおりです。
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 を取得する必要があると思います。しかし、私は間違っているかもしれません。
繰り返しますが、これに関する助けをいただければ幸いです。