Assertion failure when i use the Add Function

守給你的承諾、 提交于 2019-11-30 16:39:53

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.

return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];

At least it worked for me

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!