2012-08-23 13 views
5

問題が発生しています。どこから来たのかわからない、CoreDataに関連しています。私のデータベースでは、(1対多の関係を使用して)要素を含む一連のカテゴリ(名前と説明付き)を持っています。NSFetchedResultsControllerには0のセクションがあります

Categoryクラスの属性を与えられたセクションでテーブルビューを分割したいのですが、sectionNameKeyPath:を使用しようとすると、NSFetchedResultsControllerセクションは0セクションになります。このパラメータにnilを渡すと、1つのセクションがあります。

コードは次のとおりです。

- (NSFetchedResultsController*) fetchedResultsController 
{ 
    if(fetchedResultsController) 
     return fetchedResultsController; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" 
               inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    // Set the batch size to a suitable number. 
    [fetchRequest setFetchBatchSize:10]; 

    // Edit the sort key as appropriate. 

    NSSortDescriptor *checkDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checked" 
                    ascending:YES]; 
    NSSortDescriptor *indexDescriptor = [[NSSortDescriptor alloc] initWithKey:@"orderIndex" 
                    ascending:YES]; 
    NSArray *sortDescriptors = @[checkDescriptor, indexDescriptor]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                    managedObjectContext:self.managedObjectContext 
                    sectionNameKeyPath:@"checked" 
                       cacheName:nil]; 

    NSError *error = nil; 
    if (![fetchedResultsController performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
     return nil; 
    } else { 
     fetchedResultsController.delegate = self; 
     return fetchedResultsController; 
    } 
} 

答えて

1

NSFetchedResultsControllerのドキュメントを参照してください:(あなたのケースでは「名前」)sectionNameKeyPathするための鍵は、最初のソート記述子で使用したのと同じキーである必要があります(「チェックします」あなたの場合)。それらは異なる場合がありますが、両方のキーで同じ相対順序を生成する必要があります。

あなたの場合、「名前」に追加のソート記述子を追加し、それを最初のソート記述子として使用することを前提とします。

+0

0個のセクションを生成し続けます... – gskbyte

+0

fetchedResultsControllerを@synthesizeしましたか?私も同様の問題があり、これが問題でした。 – Giovanni

関連する問題