我被困在如何将 groupBy 与 MagicalRecord 一起使用。
我有一个有场地的国家列表
Country -<< Venues
我需要按国家/地区对所有场地进行分组,并按名称对国家/地区进行排序。
但我不确定如何使用 MagicalRecord 做到这一点。
我曾尝试使用 aNSFetchedController
但有时它会崩溃,说数组是 nil 或 0 长度。
其他时候,当有多个类别时,它只会看到 1 个类别。
最后,我不确定如何在实体上执行提取。
IE;
- (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)一个部分返回了部分名称键路径“国家”的零值。对象将被放置在未命名的部分
似乎没有看到不同的国家,我认为我没有正确完成 GroupBy 命令。
因此,我如何使用 MagicalRecord 执行 GroupBy 命令?
非常感谢。