Assertion failure when i use the Add Function

前端 未结 3 685
梦如初夏
梦如初夏 2021-01-03 15:31

In a table, I have 3 indexes which calls each a function. In each function, there is an add button that adds data into CoreData. First 2 works, but when i press the 3rd inde

相关标签:
3条回答
  • 2021-01-03 16:18

    The error message tells you the problem:

    Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)**

    In your table section (0), you started with zero rows i.e. an empty section. Then you added 1 row so the section(0) row count should be 1 but numberOfRowsInSection: is returning zero.

    There are three probable causes for this problem:

    1. You didn't freeze the tableview with beginUpdate in controllerWillChangeContent: such that the tableview tries to redraw itself before the update completes.
    2. Your numberOfRowsInSection returns the wrong row count for the section.
    3. You are inserting one managed object multiple times or you have a validate on insert/update that fails.

    Can't tell you the actual problem without the code.

    0 讨论(0)
  • 2021-01-03 16:19

    I faced the same error. I was using the delegate of NSFetchedResultsControllerDelegate. I comment rest of the delegate except the below one.

    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {  
     self.tableView.reloadData()
     if self.fetchedResultsController.fetchedObjects?.count == 0 {
        } else {
      }
    }
    

    If you want to use all delegate then you need properly manage rows and section. This error occur when you don't manage rows and section properly.

    0 讨论(0)
  • 2021-01-03 16:21
    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
    

    At least it worked for me

    0 讨论(0)
提交回复
热议问题