groupBy を MagicalRecord で使用する方法に行き詰まっています。
会場のある国のリストがあります
Country -<< Venues
すべての会場を国別にグループ化し、国を名前で並べ替える必要があります。
しかし、MagicalRecord でこれを行う方法がわかりません。
を使用しようとしましたNSFetchedController
が、配列が nil または長さ 0 であると言ってクラッシュすることがあります。
また、複数のカテゴリがある場合でも、1 つのカテゴリしか表示されない場合があります。
最後に、エンティティでフェッチを実行する方法がわかりません。
すなわち;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_objects = [NSMutableArray array];
self.fetchedResultsController = [self fetchedResultsController];
[Venue performFetch:self.fetchedResultsController];
// At this point how do I make the Venue findAllSortedBy work on the performFetch?
_objects = [NSMutableArray arrayWithArray:[Venue findAllSortedBy:@"name" ascending:YES inContext:[NSManagedObjectContext defaultContext]]];
self.title = @"Venues";
}
- (NSFetchedResultsController *)fetchedResultsController {
if (!fetchedResultsController) {
fetchedResultsController = [Venue fetchAllSortedBy:@"name"
ascending:YES
withPredicate:nil
groupBy:@"country"
delegate:self];
}
return fetchedResultsController;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSLog(@"Section = %d", section);
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(@"sectionInfo = %@", sectionInfo);
return @"Header";
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Venue *v = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = v.name;
}
これを正しく行っているかどうかはわかりません。
上記はすべてを1つの国に配置し(複数ある場合)、ログが報告されます。
CoreData: エラー: (NSFetchedResultsController) セクションが、セクション名のキー パス 'country' に対して nil 値を返しました。オブジェクトは名前のないセクションに配置されます
さまざまな国が表示されないようで、GroupBy コマンドを正しく実行したとは思えません。
では、MagicalRecord で GroupBy コマンドを実行するにはどうすればよいでしょうか?
どうもありがとう。