NSFetchedResultsController部分问题的经典案例,我正在拉我的头发。我已将 sectionNameKeyPath 设置为 NIL ,我读过的是没有任何部分的内容(即1部分)。
代码如下:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MyObjectType" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"accountExpenseTypeCache"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
正如您所看到的,我的 sectionNameKeyPath 设置为nil和
有时当我添加或删除行时,它会起作用。在尝试删除或添加时,我通常会在控制台中遇到严重的崩溃:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:955
(gdb) continue
2011-03-14 18:00:58.104 MyApplicationTest[5741:207] Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted). with userInfo (null)
问题是什么?我在4.0之前阅读NSFetchedResultsController是为了处理用户驱动的更新?我只能假设添加和删除行被认为是用户驱动的更新?我需要实现这样的东西吗?
How to implement re-ordering of CoreData records?
??从我读过的内容我不明白为什么我有这个问题,因为我的部分没有。为什么说这是3个部分?!
提前致谢!
答案 0 :(得分:1)
tableview中的节数不依赖于获取的结果控制器的sectionNameKeyPath
,而是取决于数据源numberOfSectionsInTableView:
返回的值。
您可以通过在更改数据之前不向tableview发送beginUpdate
消息来触发您获得的错误。当数据计数不断变化时,tableview可能会尝试重绘自身。
您还必须在tableview的数据源对象中实现获取的结果控制器的委托方法,以便在发生更改时发信号通知表。