我有一个管理员和员工的模型。经理与员工有关系。我有一个使用FRC的UITableViewController。 FRC在Manager模型上初始化,名称上带有排序描述符。
正常的工作 - 添加经理和删除经理按预期工作。但是,奇怪的是,当现有经理的员工更新(更改员工的某些coredata属性)时,FRC会向正在更新的员工的经理发送插入信息。 CoreData显然感到困惑,并报告了一个断言:
CoreData:错误:严重的应用程序错误。抓住了一个例外 在调用期间来自NSFetchedResultsController的委托 -controllerDidChangeContent :.无效更新:第0部分中的行数无效。现有部分中包含的行数 更新后(8)必须等于包含的行数 更新前的那一节(8),加上或减去行数 从该部分插入或删除(2个插入,1个删除)和加号 或减去移入或移出该部分的行数(0移动 in,0搬出去了)。 with userInfo(null)
奇怪的是,在断言中,似乎认为2次插入是用1次删除执行的。我记录了didChangeObject
,我只看到一个插入。
为什么在更新员工时会为Manager发送插入内容?
更新 - 显示代码
这是我的视图控制器初始化FRC。请注意,我正在为公司建模,因此管理者属于公司(这解释了谓词:
let fetchRequest = NSFetchRequest(entityName: "ManagerModel")
fetchRequest.predicate = NSPredicate(format: "%K == %@", "company", self.currentCompany)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: false)]
这是我的视图控制器的didChangeObject
功能:
func controller(controller: NSFetchedResultsController,
didChangeObject anObject: NSManagedObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?)
{
let manager = anObject as! ManagerModel
switch(type)
{
case NSFetchedResultsChangeType.Insert:
log.info("Inserting row at index path \(newIndexPath!), \(manager.name)")
break
case NSFetchedResultsChangeType.Delete:
log.info("Deleting row at index path \(indexPath!), \(manager.name)")
break
case NSFetchedResultsChangeType.Update:
log.info("Updating row at index path \(indexPath!), \(manager.name)")
break
case NSFetchedResultsChangeType.Move:
log.info("Moving row at index path \(indexPath!) to \(newIndexPath!), \(manager.name)")
break
}
}
我的代码同步数据并更新数据。以下是通过首先搜索员工来更新员工的代码。
let cdEmp = cdMgr.findEmpoyeeById(mailId: downloadedEmp.empId)
if (cdEmp != nil)
{
// If I don't update the cdEmp model, I have no problems.
// If I update any of the cdEmp properties, I'm getting
// the insert notification on the manager instance cdMgr
cdEmp!.name = downloadedEmp.name
}