squarehippo10
squarehippo10

Reputation: 1945

CoreData - Invalid number of rows using fetched controller

I know this is a common error, but I don't understand why it's happening here. In the first vc, when an add button is pressed, the user is segued to a second vc to create a new record. In the second vc everything goes along fine until I try to save the context. At that point, the first vc tries to insert the new record and the app crashes without saving.

Second VC:

private func createNewRecord() {
   let newRecord = Record(context: context)
   newRecord.recordNumber = "xxxx"
   currentCustomer?.addToRecords(newRecord)
   coreData?.saveContext() //App tries to insert new record on first VC here and crashes
   fetchCurrentRecord()
} 

First VC:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
   switch type {
   case .insert:
      if let insertIndexPath = newIndexPath {
         recordTableView.insertRows(at: [insertIndexPath], with: .fade) //CRASH!!
      }
etc.

tableView methods (first VC):

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return recordFetchedController.fetchedObjects?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! RecordCell
   let record = recordFetchedController.fetchedObjects![indexPath.row] as Record
   cell.recordNumber.text = record.recordNumber
   return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   performSegue(withIdentifier: "EditRecordSegue", sender: self)
   tableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
   if editingStyle == .delete {
      let record = recordFetchedController.fetchedObjects![indexPath.row] as Record
   context.delete(record)
   coreData?.saveContext()
   }
}

FetchedResultsController:

func configureFetchedController(searchString: String) {
   let customerFetchRequest = NSFetchRequest<Record>(entityName: "Record")

   let predicate = NSPredicate(format: "customer.name CONTAINS[c] %@", searchString)
   customerFetchRequest.predicate = predicate

   let firstSortDescriptor = NSSortDescriptor(key: "dateModified", ascending: false)
   customerFetchRequest.sortDescriptors = [firstSortDescriptor]

   recordFetchedController = NSFetchedResultsController<Record>(
   fetchRequest: customerFetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)  

   recordFetchedController.delegate = self
}

And here is the error: The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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)

Upvotes: 1

Views: 227

Answers (2)

Fattie
Fattie

Reputation: 12296

One cause of this horrible problem!

In short, I happened to have a base class for the table view I had forgotten about. As it happened, it had an overload for reloadData ...

override func reloadData() {
    super.reloadData()
    
    if isLoading {
        emptee.removeFromSuperview()
        return
    }
    
    if dataSource?.tableView(self, numberOfRowsInSection: 0) == 0 {
        addSubview(emptee)
    }
    else {
        emptee.removeFromSuperview()
    }
}

As you can see, it just relates to an "empty message"

In the code, it does call for numberOfRowsInSection ...

Unfortunately, I had the call like this:

override func reloadData() {
    // my code ...
    super.reloadData()
}

rather than like this!

override func reloadData() {
    super.reloadData()
    // my code ...
}

That was the entire problem.

Upvotes: 1

squarehippo10
squarehippo10

Reputation: 1945

The problem turned out to be completely unrelated to my fetched controller code. There is a separate customer table that sends a notification when a new customer is selected. The second VC responds to this notification by going back to the first VC. Which was good. But later I decided the customer table should move the currently selected customer to the top when a new record was being added. This had the unintended consequence of sending another notification which resulted in the second VC starting to create a new record but then getting dismissed immediately. And crash. It makes me think that I need to learn more about testing.

Upvotes: 1

Related Questions