I am having issues with fetchController update methods chapter 5

Dear Team,
I am implementing tableview update thru delegate methods.
I have type all the code for update cell and section too and adding an underdog is about to begin.

But I am having the issue with the update.
When Australia has more wins it doesn’t move up and replaces Iran but the second place that was Australia prior still become Iran with 3 wins and the first one still stays Iran(3wins). which seems that Iran comes down as it has same wins but the first place doesn’t get an update with Australia(3 wins)

Underlaying data is change as I run the app again. It shows up in the correct order as expected. Aus(3 wins) then Iran(3 wins)

extension ViewController: NSFetchedResultsControllerDelegate {
  func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
      tableView.beginUpdates()
  }
  
  func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>,
                  didChange anObject: Any,
                  at indexPath: IndexPath?,
                  for type: NSFetchedResultsChangeType,
                  newIndexPath: IndexPath?) {
    switch type {
    case .insert:
      tableView.insertRows(at: [indexPath!], with: .automatic)
    case .delete:
      tableView.deleteRows(at: [indexPath!], with: .automatic)
    case .update:
      let cell = tableView.cellForRow(at: indexPath!) as! TeamCell
      configure(cell: cell, for: indexPath!)
    case .move:
      tableView.deleteRows(at: [indexPath!], with: .automatic)
      tableView.insertRows(at: [indexPath!], with: .automatic)
    @unknown default:
      print("Unexpected NSfetchResultChangeType")
      
    }
  }
  
  func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
      tableView.endUpdates()
  }
  
  func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>,
                  didChange sectionInfo: NSFetchedResultsSectionInfo,
                  atSectionIndex sectionIndex: Int,
                  for type: NSFetchedResultsChangeType) {
    let indexSet = IndexSet(integer: sectionIndex)
    
    switch type {
    case .insert:
      tableView.insertSections(indexSet, with: .automatic)
    case .delete:
      tableView.deleteSections(indexSet, with: .automatic)
    default: break
    }
  }
  
}

Never mind. Found the issue. I never used newIndexPath when inserting rows

Hence Case closed.

@alokc83 Glad you sorted it out! Cheers! :]

This topic was automatically closed after 166 days. New replies are no longer allowed.