NSFetchedResultsControllerのsectionNameKeyPath
CoreData の NSFetchedResultsControllerのsectionNameKeyPathを指定することで、指定したカラム名でセクション分けをすることが可能。
ただ、NSSortDescriptorも同じカラムを優先してソートしないと、セクションわけがおかしくなる?
ちょっと結果だけ見ての判断なので詳しいことはわかりませんが、そんな結論にたっしました。
意図した動作のコード
use_section_idの値でセクション分けして、さらにセクション内はother_order_indexでソートする
NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:self.modelName] ; NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"use_section_id" ascending:YES]; NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"other_order_index" ascending:YES]; [req setSortDescriptors:[[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil]] ; NSFetchedResultsController *res = [[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:self.moc sectionNameKeyPath:@"use_section_id" cacheName:nil] ; NSError *error = nil ; [res performFetch:&error] ;
うまく行かなかったコード
どのようにうまく行っていないかはわかっていない。
NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:self.modelName] ; NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"other_order_index" ascending:YES]; [req setSortDescriptors:[[NSArray alloc] initWithObjects:sortDescriptor1, nil]] ; NSFetchedResultsController *res = [[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:self.moc sectionNameKeyPath:@"use_section_id" cacheName:nil] ; NSError *error = nil ; [res performFetch:&error] ;