这是我的控制器类的接口:
@interface ProjectListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
@end
我使用以下代码初始化 fetchedResultsController :
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create and configure a fetch request with the Project entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *projectIdDescriptor = [[NSSortDescriptor alloc] initWithKey:@"projectId" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:projectIdDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
如您所见,我使用的是我的控制器类中定义的 managedObjectContext
这是采用NSFetchedResultsControllerDelegate协议:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self _configureCell:(TDBadgedCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
if (newIndexPath != nil) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.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];
}
在_configureCell:atIndexPath:方法内部我有以下代码:
NSFetchRequest *issuesNumberRequest = [NSFetchRequest new];
NSEntityDescription *issueEntity = [NSEntityDescription entityForName:@"Issue" inManagedObjectContext:managedObjectContext];
[issuesNumberRequest setEntity:issueEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"projectId == %@", project.projectId];
[issuesNumberRequest setPredicate:predicate];
NSUInteger issuesNumber = [managedObjectContext countForFetchRequest:issuesNumberRequest error:nil];
[issuesNumberRequest release];
我再次使用managedObjectContext。
但是当我尝试插入新项目时,应用程序崩溃并出现以下异常:
断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-984.38 / UITableView.m:774 由于未被捕获而终止应用程序 例外 'NSInternalInconsistencyException', 原因:'无效更新:无效 第0节中的行数 一个包含的行数 更新后的现有部分(4) 必须等于行数 包含在该部分之前 更新(4),加号或减号 从中插入或删除的行数 部分(1个插入,0个删除)。'
幸运的是,我找到了一个解决方法:如果我在_configureCell中创建并使用单独的NSManagedObjectContext:atIndexPath:方法应用程序不会崩溃!
我只想知道,这种行为是否正确?
Here is a sample project. 请注意CrashMeViewController的'_configureCell:atIndexPath:'和'_addSomeRows'方法
答案 0 :(得分:0)
已在iOS 4.0中修复