患者と仕事の 2 つのエンティティがあります。患者は「ジョブ」と呼ばれるジョブと対多の関係を持ち、ジョブは「患者」と呼ばれる患者と対一の関係を持ちます。Job には「dueDate」(Date) と「completed」(BOOL) という属性があり、Patient には属性「firstName」と「lastName」(どちらも文字列) があります。
NSFetchedResultsController のフェッチ要求/述語を作成しようとしています。完了していないすべてのジョブを取得し (つまり、完了 == NO)、それらを患者名でセクション化します。これが私のコードです:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Job" inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(completed == NO)"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *patientDescriptor = [[NSSortDescriptor alloc] initWithKey:@"patient" ascending:YES];
NSSortDescriptor *dueDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dueDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:patientDescriptor, dueDateDescriptor, nil];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:@"patient" cacheName:@"Jobs"];
ここに私の titleForHeaderInSection メソッドがあります:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
NSString *firstName = [[(Job *)[fetchedResultsController objectAtIndexPath:indexPath] patient] firstName];
NSString *lastName = [[(Job *)[fetchedResultsController objectAtIndexPath:indexPath] patient] lastName];
return [NSString stringWithFormat:@"%@ %@", firstName, lastName];
これはうまくいかないようです。私はこれについて間違った方法で進んでいますか?