0
votes

I get a NSInternalInconsistencyException exception. It says : 'Invalid update: invalid number of rows in section 0. 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).

It says before the update numberOfRowsInSection was 1, but it is not how I can see during debugging. Before the update, last time when numberOfRowsInSection was called it returned 0.

Do you have any idea?

this is what I see:

  1. numberOfRowsInSection return 0
  2. self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
  3. numberOfRowsInSection get called and return 1
  4. crash
1
Error is saying that, there is 1 row before update and after update it should be 2, but it is not 2 - bhargavg

1 Answers

-1
votes

my solution has two things: (1) add beginUpdates / endUpdates (2) add a bool variable to check whether view was already loaded or not

more explanation: if beginUpdates triggers a view reload cycle then viewcontroller will note that datasource already increased, so no insertRowsAtIndexPaths needs, if beginUpdates not triggers view reload cycle then neigter numberOfRowsInSection will be called so insertRowsAtIndexPaths can be called

func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {

        if controller == frc {

            switch(type) {
            case .Insert:

                isLayoutReloaded = false
                self.tableView.beginUpdates() //<- !!!
                if !isLayoutReloaded {        //<- !!!
                    self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)

                }
                self.tableView.endUpdates()   //<- !!!
            }
        }
    }

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

    isLayoutReloaded = true                   //<- !!!

    let numberOfRowsInSection = ((frc!.sections as NSArray)[0] as NSFetchedResultsSectionInfo).numberOfObjects
    return numberOfRowsInSection
}