【发布时间】:2016-06-28 09:41:24
【问题描述】:
我的问题是我收到 CoreData 严重错误,但我不知道为什么。错误类似于:
Core Data Serious Application Error at controllerDidChangeContent
我有一个控制器显示用户列表(存储在 CoreData 的用户表中)。非常直接。
在第二个视图控制器中,我从服务器检索用户列表,并检索存储在 CoreData 中的用户,以查看本地是否已经有相同的用户(在这种情况下,我更改单元格的颜色背景)我还在第一部分的第一个单元格中添加了一个带有标题的标题。
这是我在第一个 viewController init 构造函数中初始化 fetchController 的方式:
let modelMethodNameForSection = "firstCharOfFirstname"
let requestHelper = NSFetchRequest(entityName: modelEntityClass)
requestHelper.predicate = NSPredicate(format: "friend.groupA == YES")
requestHelper.sortDescriptors = [sortDescriptor]
let managedObjectCtx = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
fetchedResultsController = NSFetchedResultsController(fetchRequest: requestHelper, managedObjectContext: managedObjectCtx, sectionNameKeyPath: modelMethodNameForSection, cacheName: nil)
以下是代表:
//MARK: - NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller: NSFetchedResultsController)
{
tableView!.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController)
{
tableView!.endUpdates()
if let fetchedObjects = controller.fetchedObjects {
segmentedControlDelegate?.updateSegment(fetchedObjects.count, listType: ProfilePeopleListType.Friends) //listType doesn t matter here
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
{
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
tableView!.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break
case .Delete:
if let indexPath = indexPath {
tableView!.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break
case .Update:
if let indexPath = indexPath {
if let cell = tableView!.cellForRowAtIndexPath(indexPath) as? UserViewCell { //We need to check that it's a UserViewCell, because it can be another class
configureCell(cell, atIndexPath: indexPath)
}
}
break
case .Move:
if let indexPath = indexPath {
tableView!.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if let newIndexPath = newIndexPath {
tableView!.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
break
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
{
switch type {
case .Insert:
tableView!.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
tableView!.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
这是第二个 viewController 显示与服务器合并的结果的代码:
let modelMethodNameForSection = "firstCharOfFirstname"
let requestHelper = NSFetchRequest(entityName: modelEntityClass)
requestHelper.predicate = NSPredicate(format: "friend.groupA == YES")
requestHelper.sortDescriptors = [sortDescriptor]
let managedObjectCtx = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
fetchedResultsController = NSFetchedResultsController(fetchRequest: requestHelper, managedObjectContext: managedObjectCtx, sectionNameKeyPath: modelMethodNameForSection, cacheName: nil)
我的表格视图在section[0]的行[0](表格顶部)中显示包含标题的标题
//MARK: - NSFetchedResultsControllerDelegate
//When the local database Friends table changes we are notified
func controllerWillChangeContent(controller: NSFetchedResultsController)
{
}
func controllerDidChangeContent(controller: NSFetchedResultsController)
{
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
{
//We refresh the lists with new data from local Database
let listFriendsFilterBlock = peopleListWithOwnerFriendsFilter()
tableView?.reloadData()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
{
//We don't have any sections in the users list merged with server data
}
编辑 崩溃日志:
[;2016-03-16 18:51:10.511 *** -[UITableView 中的断言失败 _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/UITableView.m:1720 2016-03-16 18:51:10.511 CoreData:错误:严重的应用程序错误。 从 的代表处捕获了一个异常 NSFetchedResultsController 在调用期间 -controllerDidChangeContent:。无效更新:第 0 节中的行数无效。现有节中包含的行数 更新后(2)必须等于包含的行数 更新前的那个部分(1),加上或减去行数 从该部分插入或删除(0 插入,0 删除)和加上 或减去移入或移出该部分的行数(0 移动 入,0 移出)。与 userInfo (null)
【问题讨论】:
-
告诉我们您收到的严重错误消息
-
在这里,行数有问题。
-
知道 NSFetchedResultsController 甚至不知道 tableView (我们甚至没有在代码中给出指向它的指针),我真的不明白异常是如何发生的
标签: ios swift uitableview core-data nsfetchedresultscontroller