0

UITableView使用して Core Data から を入力していNSFetchedResultsControllerます。データは、NSManagedObjectMenuItem と呼ばれる Mogenerator によって生成されたサブクラスから取得されます。MenuItem エンティティには SectionID パラメータがありNSNumber、これを使用して、テーブル ビュー内のどのセクションに項目を配置するかを決定します。

データに対してテスト フェッチを実行し、コア データが正しく設定されていることを確認しました。すべて問題ありませんでした。

NSFetchedResultsController次のように作成されsectionKeyNamePath、 @"sectionId" に設定されます。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MenuItem" inManagedObjectContext:self.persistenceController.moc];
[fetchRequest setEntity:entity];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"sectionId" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"rowId" ascending:YES]];    
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                          managedObjectContext:self.persistenceController.moc
                                            sectionNameKeyPath:@"sectionId"
                                                     cacheName:nil];

問題(および解決策):

このコードでは、セクションは識別されません。はNSFetchedResultsController常に 0 を返します。これは以前に NSMO サブクラスを手動で作成したときに機能していたので、Mogenerator に関連するものであると考えました。

sectionNameKeyPath を @"primitiveSectionId" に変更すると、機能します。

これが将来誰かに役立つことを願っていますが、ここで何が起こっているのかわかりません。これで問題が解決する理由を誰か説明してください。

ありがとう

4

1 に答える 1

0

FRC を nil に設定するのはなぜですか? これを試してください(NSNumber属性を使用して機能します):

mogenerator を使用すると、たとえば、"[MenuItem entityName]" を使用してエンティティを取得し、MenuItemAttributes.sectionId を使用して属性にアクセスできます。

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MenuItem entityName]];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:MenuItemAttributes.sectionId ascending:YES], [NSSortDescriptor sortDescriptorWithKey:MenuItemAttributes.rowID ascending:YES]];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                      managedObjectContext:self.persistenceController.moc
                                                                        sectionNameKeyPath:MenuItemAttributes.sectionId
                                                                                 cacheName:nil];

// Make sure you set your frc as the delegate
frc.delegate = self;

NSError *error = nil;
if (![fetcher performFetch:&error]) {
    /*
     * Did you execute the request?
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
NSLog(@"%lu", [frc.sections count]);
于 2015-10-05T02:46:44.687 に答える