1
votes

I am facing a crash issue while inserting a row to tableview.This happens when pagination reload(Very Fast scrolling of tableview) table view and inserting row for ads happens almost similar time.

Ad inserting method

[self.news insertObject:adStory atIndex:index];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]];
[[self tableView] beginUpdates]; 
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[[self tableView] endUpdates];

OperationDidfinish

[_tableView relaoddata];

Crash Logs

*** 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 (45) must be equal to the number of rows contained in that section before the update (43), 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).'

    *** First throw call stack:
  (0   CoreFoundation                      0x000000010e8ebb0b __exceptionPreprocess + 171

1   libobjc.A.dylib                     0x000000010e357141 objc_exception_throw + 48

2   CoreFoundation                      0x000000010e8efcf2 +[NSException raise:format:arguments:] + 98
3   Foundation                          0x00000001088413b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4   UIKit                               0x000000010c8d78e8 -[UITableView _endCellAnimationsWithContext:] + 16362
5   UIKit                               0x000000010c8ee9cc -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 329
2
please post some crash logs. - Pranjal Bikash Das
added the crash log.please check - Angel
What is relaoddata ? Basically do not call insertRows.../deleteRows... and reloadData simultaneously. insertRows... does update the UI. And beginUpdates / endUpdates has no effect when performing a single insert/delete/move operation. - vadian
relaoddata is in another function. After getting next list of stories from Api. - Angel

2 Answers

0
votes

Any UI related changes must be executed on the main thread. Making UI changes on the background thread might result in slowing down the changes.

Try this,

dispatch_async(dispatch_get_main_queue(), ^{
        [self.news insertObject:adStory atIndex:index];
        NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]];
        [[self tableView] beginUpdates];
        [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
        [[self tableView] endUpdates];
    });

This will take care of moving the changes to main thread if required.

0
votes

Please make sure that number of rows returned in the delegate method - (NSInteger)numberOfRowsInSection:(NSInteger)section; is equivalent to that of, after you insertRowsAtIndexPaths and endUpdates. E.g if earlier numberOfRows is 4 and you insert one row, than numberOfRows should return 4+1. Else it will be mismatch in endUpdate.

Following crash log describes that only.

*** 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 (45) must be equal to the number of rows contained in that section before the update (43), 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).'