我有以下代码:
private lazy var fetchedResultsController: FetchedResultsController<Worker> = {
let fetchRequest = NSFetchRequest(entityName: Worker.entityName)
let workerTypeSortDescriptor = NSSortDescriptor(key: "workerType", ascending: true)
let firstNameSortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
let lastNameSortDescriptor = NSSortDescriptor(key: "lastName", ascending: true)
fetchRequest.sortDescriptors = [workerTypeSortDescriptor, firstNameSortDescriptor, lastNameSortDescriptor]
let context = CoreDataStackSingleton.sharedInstance.managedObjectContext
let frc = FetchedResultsController<Worker>(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "workerType")
frc.setDelegate(self.frcDelegate)
return frc
}()
如果我的表是空的,我添加导演(workerType = 0)的人,一切都好。然后我添加员工(workerType = 1)或簿记员(workerType = 2)的人。然后我收到了这个错误:
CoreData:错误:严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常。无效更新:第0节中的行数无效。更新(2)后现有部分中包含的行数必须等于更新前该部分中包含的行数(1),加上或减去数字从该部分插入或删除的行( 0插入,0删除)和加或减移入或移出该部分的行数( 0移入,0移出)。 with userInfo(null)
事实证明,新人已添加到现有部分,但应为此创建新部分。 这是我的项目:link