Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView: Recovering from NSInternalInconsistencyException after bad updates?

So I'm updating a tableview by inserting/deleting/reloading rows as needed, but, as I'm not 100% confident that the tableview will always update correctly, is there any way to fail safely from a bad batch of updates?

Right now, I have this:

    // Try to animate the updates. If something goes wrong, just reloadData.
    @try {
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:deleteArray withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationNone];
        [tableView insertRowsAtIndexPaths:insertArray withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView endUpdates]; 
    }
    @catch (NSException * e) {
        if([[e name] isEqualToString:NSInternalInconsistencyException]){    
            [tableView reloadData];
            NSLog(@"animation failed, just reloading data");
        }
        else {
            @throw e;
        }
    }

However, once it hits that exception, reloadData seems to not work. Is there any other way to basically reset the UITableView into a working state?

like image 943
David Liu Avatar asked Jun 28 '10 21:06

David Liu


1 Answers

More ideally you should be backing the table with an array that's guaranteed to do what the table expects it to do. UIKit expects exceptions to be fatal (this holds with Apple's philosophy that exceptions indicate programmer error.)

like image 88
cobbal Avatar answered Sep 18 '22 14:09

cobbal