2

dueDate 属性を持つマネージド オブジェクトがあります。UITableView のセクション ヘッダーとして醜い日付文字列を使用して表示する代わりに、「category」という一時的な属性を作成し、次のように定義しました。

- (NSString*)category
{
    [self willAccessValueForKey:@"category"];

    NSString* categoryName;
    if ([self isOverdue])
    {
        categoryName = @"Overdue";
    }
    else if ([self.finishedDate != nil])
    {
        categoryName = @"Done";
    }
    else
    {
        categoryName = @"In Progress";
    }

    [self didAccessValueForKey:@"category"];
    return categoryName;
}

NSFetchedResultsController のセットアップは次のとおりです。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task"
                                          inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSMutableArray* descriptors = [[NSMutableArray alloc] init];
NSSortDescriptor *dueDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dueDate"
                                                                  ascending:YES];
[descriptors addObject:dueDateDescriptor];
[dueDateDescriptor release];
[fetchRequest setSortDescriptors:descriptors];

fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"category" cacheName:@"Root"];

テーブルは最初は正常に表示され、「進行中」というタイトルのセクションに dueDate が渡されていない未完成のアイテムが表示されます。これで、ユーザーはテーブル ビューの行をタップして、新しい詳細ビューをナビゲーション スタックにプッシュできます。この新しいビューで、ユーザーはボタンをタップして、アイテムが「完了」したことを示すことができます。ボタンのハンドラーは次のとおりです (self.task は管理対象オブジェクトです)。

- (void)taskDoneButtonTapped
{
    self.task.finishedDate = [NSDate date];
}

「finishedDate」属性の値が変更されるとすぐに、次の例外が発生します。

2010-03-18 23:29:52.476 MyApp[1637:207] Serious application error.  Exception was caught during Core Data change processing: no section named 'Done' found with userInfo (null)
2010-03-18 23:29:52.477 MyApp[1637:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no section named 'Done' found'

データセットで何かが変更されたことが NSFetchedResultsController に通知されたため、新しい詳細ビューによって現在非表示になっている UITableView が行とセクションを更新しようとしていることがわかりました。これが私のテーブル更新コードです (Core Data Recipes サンプルまたは CoreBooks サンプルのいずれかからコピーしたもので、どちらを思い出せません)。

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

これらの各関数にブレークポイントを設定したところ、controllerWillChange のみが呼び出されることがわかりました。controller:didChangeObject:atIndexPath:forChangeType:newIndex または controller:didChangeSection:atIndex:forChangeType が呼び出される前に、例外がスローされます。

この時点で私は立ち往生しています。sectionNameKeyPath を単に「dueDate」に変更すると、すべて正常に動作します。これは、dueDate 属性が変更されないのに対し、finishedDate 属性が変更された後に読み返すとカテゴリが異なるためだと思います。

助けてください!

アップデート:

ここに私の UITableViewDataSource コードがあります:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    [self configureCell:cell atIndexPath:indexPath];    

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];    
    return [sectionInfo name];
}
4

2 に答える 2

2

クラッシュは、NSFetchedResultsControllerが事前に「完了」カテゴリを認識していないためにクラッシュすることが原因です。私は他の質問でこのクラッシュを何度か見たことがあり、それぞれについてAppleにレーダーチケットを提出することをお勧めします。これはのバグですNSFetchedResultsController

于 2010-03-27T14:32:36.893 に答える
1

あなたの問題は、あなたが提供するために使用している「カテゴリ」一時プロパティにあるようですsectionNameKeyPathsectionNameKeyPathプライマリ ソート記述子と同じ順序にする必要があります。あなたの場合、これは、すべての「期限切れ」タスクがすべての「完了」タスクよりも前の日付を持たなければならないことを意味し、すべての「進行中」タスクよりも前の日付を持たなければなりません。dueDate「完了」タスクに「進行中」タスクの後または「期限切れ」タスクの前に来るシナリオを構築することができます。このシナリオは、 の順序要件を破り、がをスローするsectionNameKeyPath原因になります。NSFetchedResultsControllerNSInternalConsistencyException

セクションに分割する必要がある独自の配列をローリングする必要のない問題の解決策を提案します。モデルに整数属性を作成し、0 を「期限切れ」、1 を「完了」、2 を「進行中」にマップします。これをプライマリ ソート記述子にしてNSFetchRequest、このプロパティを昇順でソートします。プロパティを昇順NSFetchRequestで並べ替えるセカンダリ 並べ替え記述子を に追加します。dueDate上記で作成した整数属性からカテゴリ名を取得し、それを として使用するようにカテゴリ メソッドを変更しますsectionNameKeyPath。タスクが進行中から期限切れ、完了などに移動するときに、整数属性を更新してタスクを更新する必要があります。

于 2010-03-27T08:22:09.693 に答える