0

我被困在如何将 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 命令?

非常感谢。

4

2 回答 2

2
self.fetchedVenues = [Venue MR_fetchAllGroupedBy:@"country" withPredicate:nil sortedBy:@"name" ascending:YES];
于 2014-02-23T23:03:02.467 回答
1

此错误告诉您,在您的所有 Venue 对象中,您的结果集中至少有一个没有“国家”值。您需要验证您确实填写了此字段并在获取之前正确保存。

仅供参考,在您的 viewDidLoad 方法中,您不需要所有代码。只需执行以下操作:

- (void) viewDidLoad;
{
    [super viewDidLoad];
    self.fetchedVenues = [Venue fetchAllSortedBy:@"name" ascending:YES withPredicate:nil groupBy:@"country" delegate:self];
}

fetchAllSortedBy... 将为您执行提取,并记录错误等。这就是像 MagicalRecord 这样的帮助框架的要点。

于 2013-09-05T04:39:54.530 回答