TableViewとNSFetchedResultsControllerを持つビューがあります。 私はJSONフィードをダウンロードして解析し、対応するエンティティをコアデータに挿入するためにASINetworkQueue(NSOperationQueueのサブクラス)とASIHTTPRequestのサブクラス(NSOperationのサブクラス)を使用しています。 したがって、ASIHTTPRequestサブクラスでは、すべてのスレッドセーフとniceを維持するための2番目のNSManagedObjectContextがあります。NSFetchedResultsController - 新しいセクションが作成されたときに例外が発生する(UPDATE:セクション内のすべての行が削除されたときなど...)
私のバックグラウンドフェッチ/インポートは10秒ごとに起動します。新しいエンティティが作成され、コアデータストアに保存されます。 NSNotificationは、ViewControllerとNSFetchedResultsControllerにその方法を伝播し、新しい行がTableViewに表示されます。
問題は、セクションキーの新しい値(「sectionID」と呼ぶことができます)を持つエンティティが含まれている場合に発生します。たとえば、セクションID == 1ではなくセクションID == 2です。この時点で
、NSFetchedResultsControllerは、テーブルビューは、新しいセクションを作成するべきであるが、代わりに私は例外を取得しています:ここで
Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** -[NSArray initWithObjects:count:]: attempt to insert nil object at objects[0] with userInfo (null)
Assertion failed: (_Unwind_SjLj_Resume() can't return), function _Unwind_SjLj_Resume, file /SourceCache/libunwind/libunwind-24.1/src/Unwind-sjlj.c, line 326.
はNSFetchedResultControllersのデリゲートメソッドのための私のコードです:
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[[self eventTable] beginUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSIndexSet* set = [NSIndexSet indexSetWithIndex:sectionIndex];
switch (type) {
case NSFetchedResultsChangeInsert:
[[self eventTable] insertSections:set withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[[self eventTable] deleteSections:set withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView* tv = [self eventTable];
switch (type) {
case NSFetchedResultsChangeInsert:
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self tableView:tv configureCell:[tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self eventTable] endUpdates];
}
例外を引き起こしていることについて何か考えていますか?前もって感謝します!新しいセクションが作成された、または古いものが削除されたときにエラーが発生したかどうか本当にわからない
UPDATE 2011-02-03 。私はほとんどこれがセクション内のすべての行が削除されるが、何らかの理由でコントローラ:didChangeSection:atIndex:forChangeTypeが呼び出されていないときに発生すると考えています。 似たような経験をお持ちの方?
UPDATE 2011-02-08 私はそれを解決したと思います。問題は、行/セクションを削除するかどうかを決定するときに、いくつかの余分な条件を考慮しなければならないということでした。 アップルのドキュメントで提供されているコードを使用して(私のビューで機能させるためにいくつかの調整を加えて)、それは正常に実行されます。
あなたのしたことを説明し、あなたの問題を解決することをお勧めします。 –
上記のコメントとリンクを追加しました。 –
ありがとう、Manne W! –