0

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 コマンドを実行するにはどうすればよいでしょうか?

どうもありがとう。

4

2 に答える 2

1

このエラーは、すべての Venue オブジェクトのうち、「国」の値を持たない結果セットが少なくとも 1 つあることを示しています。取得する前に、このフィールドに実際に入力して適切に保存していることを確認する必要があります。

参考までに、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 に答える