0
votes

A view controller has a data model which updates constantly and aggressively

A UItableview (storyTable) is called ever so often to update and reflect the changes to date.

(Changed the code to reflect only a single beginUpdates endUpdates) - still crashes

  [self.storyTable beginUpdates];
                if([deleteIndexPaths count]){
                    DLog(@"Table refresh delele rows :%@",deleteIndexPaths);

                    [self.storyTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationNone];


                }

                if ([addIndexPaths count]){
                    DLog(@"Table refresh add rows :%@",addIndexPaths);

                    [self.storyTable insertRowsAtIndexPaths:addIndexPaths withRowAnimation:UITableViewRowAnimationTop];

                }
                if ([changedIndexPaths count]){

                    DLog(@"Table refresh change rows :%@",changedIndexPaths);
                    [self.storyTable reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationFade ];

                }
                [self.storyTable endUpdates];
                //[self.storyTable endUpdates];
                tableIsAnimating = NO;

        }

Crashes from time to time with the following message: (mostly on deleteRowsAtIndexPaths...)

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (20) must be equal to the number of rows contained in that section before the update (20), plus or minus the number of rows inserted or deleted from that section (0 inserted, 10 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

or :

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 20 into section 0, but there are only 20 rows in section 0 after the update'

Question: How can one add some protective code to prevent the crash?

2
Is there any reason you don't want to use NSFetchedResultsController and NSFetchedResultsControllerDelegate to show the model changes? Are you not using Core Data?Gobot

2 Answers

1
votes

I think the problem here is that when there are both deletes, adds and changes mixed at the same time, your indexPaths will be all wrong after first -endUpdates.

You can try this:

[self.storyTable beginUpdates];
if([deleteIndexPaths count]){
   [self.storyTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationNone];
}
if ([addIndexPaths count]){
   [self.storyTable insertRowsAtIndexPaths:addIndexPaths withRowAnimation:UITableViewRowAnimationTop];
}
if ([changedIndexPaths count]){
   [self.storyTable reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationFade ];
}
[self.storyTable endUpdates];

This way the indexPaths will be valid through all operations.

-2
votes

i think your tableview change not because you deleterowsatindexpath but reloaddata, in the other word using deleterowateindexpath do not do the basic deleting work.