How to protect tableview from crashing during insertRowsAtIndexPaths and device rotation
ios, uitableview
Solution
You have essentially created a race condition.
The problem is you are manipulating `self.array` in a background thread while `self.tableView insertRowsAtIndexPaths` is running on the main thread and will be accessing `self.array`.
So at some point`self.tableView insertRowsAtIndexPaths` (or other tableView methods called as a result of this) is running on the main thread, expecting a certain number of objects in `self.array`, but the background thread gets in there and adds another one...
To fix your simulation:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
for (int i = 0; i < 30; i++) {
[NSThread sleepForTimeInterval:0.2]; // imitates fetching and parsing
NSString *myNewObject = [NSString stringWithFormat:@"cell number %d", i]];
dispatch_async(dispatch_get_main_queue(), ^{
// perform on main
[self.array addObject: myNewObject];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
});
}
});
Problem
This sample app will crash after some time if you constantly rotate the device during the row animations. My real app crashes sometime even on the first rotate during the row animations. How should I protect my app from crashing during rotation with simultanous row animations? Please don't suggest to forbid rotation until the animations are done. DataSource is dependent on network fetches which may take anything from 1 to 30 seconds depending on user network and user wants to rotate the device if he sees the app is better viewed in landscape for example right after launch. ``` - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ for (int i = 0; i < 30; i++) { [NSThread sleepForTimeInterval:0.2]; // imitates fetching and parsing [self.array addObject:[NSString stringWithFormat:@"cell number %d", i]]; dispatch_async(dispatch_get_main_queue(), ^{ // perform on main NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); } }); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.array.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; // Configure the cell... cell.textLabel.text = self.array[indexPath.row]; return cell; } - (NSMutableArray *)array { if (!_array) { _array = [[NSMutableArray alloc] init]; } return _array; } ``` Crash report ``` 2014-02-21 12:47:24.667 RowsAnimationRotate[2062:60b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1330 2014-02-21 12:47:24.673 RowsAnimationRotate[2062:60b] *** 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 (8) must be equal to the number of rows contained in that section before the update (8), 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).' ```