核心数据严重的应用程序错误在controllerDidChangeContent
你好,我现在堆积在下面的错误。
CoreData:错误:严重的应用程序错误。
在调用-controllerDidChangeContent:期间,NSFetchedResultsController的委托捕获到exception。
无效的更新:无效的部分数量。
更新(7)之后,表格视图中包含的部分数量必须等于更新前的表格视图中包含的部分数量(6),加上或减去插入或删除的部分数目(0插入,0删除)。 与userInfo(null)
我的应用程序有多个部分tableView。 只有当我尝试插入新节logging时才会出现错误。 如果该部分已经存在,则不会发生错误。
相关的代码如下。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; DLog(@"newIndexPath %@",newIndexPath); switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { /*NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { DLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [self.tableView reloadData];*/ [self.tableView endUpdates]; }
此外,我把sectionNameKeyPath在fetchResultsController。 实际核心数据中不存在年份列,这是扩展列。
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"year" cacheName:@"Master"];
我可以通过以下更改来解决错误。 只是为了一个人在相同的情况。
我注释了下面这行。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; DLog(@"newIndexPath %@",newIndexPath); switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; }
我添加了以下行。
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { // In the simplest, most efficient, case, reload the table view. NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { DLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [self.tableView reloadData]; }
- Swift coreData – 格式化date并在谓词中使用它
- 如何在Xcode 4.2中禁用CoreData的反向关系警告?
- 我如何使用NSManagedObject的新fetchRequest函数进行提取请求?
- Core Data不会触发NSManagedObject实例的错误,作为应用程序委托的属性
- 核心数据是一种graphics数据库吗?
- NSManagedObject setter给我:无法识别的select器发送到实例
- 为什么我应该为每个新线程或NSOperation创build一个NSManagedObjectContext,而不是在主线程上调用Core Data?
- 找不到适合核心数据迁移的映射模型
- iOS 8 Swift – 保存多个实体的数据