Добрый день
Пытаюсь обновить свою таблицу при помощи NSFetchedResultsController. Код выглядит следующим образом:
func controllerWillChangeContent(_ controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
tableView = self.tableView
switch type {
case .insert:
print("insert")
tableView.insertRows(at: [IndexPath(row: enabledArray.count, section: 0)], with: .automatic)
case .update:
print("update")
if let indexPath = indexPath {
switch indexPath.row {
case lastCell:
let cell = Bundle.main.loadNibNamed("AddAlarmCell", owner: self, options: nil)?.first as! AddAlarmCell
cell.delegate = self
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
default:
let cell = Bundle.main.loadNibNamed("AlarmCell", owner: self, options: nil)?.first as! AlarmCell
cell.delegate = self
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
cell.daysLbl.text = ""
if enabledArray[indexPath.row].mondayOn == true{
cell.daysLbl.text = "ПН "
}
if enabledArray[indexPath.row].tuesdayOn == true{
cell.daysLbl.text = cell.daysLbl.text!+"ВТ "
}
if enabledArray[indexPath.row].wednesdayOn == true{
cell.daysLbl.text = cell.daysLbl.text!+"СР "
}
if enabledArray[indexPath.row].thursdayOn == true{
cell.daysLbl.text = cell.daysLbl.text!+"ЧТ "
}
if enabledArray[indexPath.row].fridayOn == true{
cell.daysLbl.text = cell.daysLbl.text!+"ПТ "
}
if enabledArray[indexPath.row].saturdayOn == true{
cell.daysLbl.text = cell.daysLbl.text!+"СБ "
}
if enabledArray[indexPath.row].sundayOn == true{
cell.daysLbl.text = cell.daysLbl.text!+"ВС "
}
cell.timeLbl.text = enabledArray[indexPath.row].alarmTimeStr
cell.onOffSwitch.isOn = enabledArray[indexPath.row].alarmOn
}
}
case .move:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)
}
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath as IndexPath], with: .automatic)
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)
}
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.endUpdates()
}
Все изменения отрабатываются корректно, кроме функции добавления: при попытке использовать insert он выдает ошибку :
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
Я так понимаю, что дело в неправильной индексации размера массива, но где именно это может сбоить - не очень понятно. Нет ли у кого идей, в какой области надо копать?