私が間違っていなければ、軽量の移行を使用してこの種の変更を実現できます。Project エンティティと Video エンティティの間に 1 対多の順序付けられた関係を作成する必要があります。NSFetchedResultsController を使用してプロジェクトのリストを取得し、Video エンティティとの関係をたどって関連オブジェクトを取得できます。多かれ少なかれ次のようになります。
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext: context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setRelationshipKeyPathsForPrefetching: @"videos"];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest: fetchRequest
managedObjectContext: context
sectionNameKeyPath: nil
cacheName: nil];
NSFetchRequest オブジェクトを設定して、「ビデオ」関係をプリフェッチします。これにより、ビデオ エンティティにアクセスする際の時間が節約されます。次に、Project エンティティのリストを取得した後、tableView:cellForRowAtIndexPathでそれらにアクセスします。
- (NSInteger) numberOfSectionsInTableView: (UITableView*) tableView
{
return [self.fetchedResultsController.fetchedObjects count];
}
- (NSInteger) tableView: (UITablView*) tableView numberOfRowsInSection: (NSInteger) section
{
Project *project = [self.fetchedResultsController.fetchedObjects objectAtIndex: section];
return [project.videos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
Project *project = [self.fetchedResultsController.fetchedObjects objectAtIndex: indexPath.section];
Video *video = [project.videos objectAtIndex: indexPath.row];
...
}