Title: TableView: How to proceed after catching 'NSInternalInconsistencyException'?
Tags: ios;objective-c;uitableview
Question: I have an application with a ```UITableView```. The application communicates with a server.

Problem is the following:

Client 1 deletes a cell of the TableView. The data update is transmitted to the server which sends a data update to Client 2. In the same moment Client 2 deletes a cell from the TableView. Due to the receiving update an ```NSInternalInconsistencyException``` is thrown because the number of cells before and after are not as expected (difference is 2 not 1).

The app crashes in

```[tableView endUpdates];
```

Of course I can catch the exception with

```- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        @try{
            // Send data update to the server
            [sender sendDataToServer:data];

            // Update tableview
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
            [tableView endUpdates];
        }
        @catch (NSException * e) {
            // Hide red delete-cell-button
            tableView.editing = NO;
            // Jump to previous ViewController
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
```

But after the successful catch I get an

```
  EXC_BAD_ACCESS exception in [_UITableViewUpdateSupport dealloc].
```

What can I do to prevent the app from crashing? What do I have to do in the catch-block to "clean" the situation? Reloading the tableview doesn't take any effect.

EDIT: numberOfRows-method looks like this: 

```- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [dataSourceArray count];
}
```

Comment: `deleteRowsAtIndexPath` updates the view but not the model. You have to delete the object also from the data source array. Then you can remove the expection block. It will never occur again. And remove also `begin/endUpdates`. It has no effect in this case.

Comment: @Farley write your `numberOfRowsInSection` method

Comment: You don't *"clean"* it - you have to ***prevent it*** from occurring! Write your code in a way that makes these kind of exceptions impossible to happen.

Comment: Sounds like some thread synchronisation is required.  Look at `@synchronized(lock) { ... }`.

Comment: are you deleting this object from your array?

Comment: @vadian: Yes, i delete object also from data source array. Crashes anyway.

Here is the accepted answer: @synchronized(lock) solved my problem. 

I just had to make sure that only one thread is able to manipulate the data source array at the same time. Now everything works fine, no more 'NSInternalInconsistencyException'.

Thx to trojanfoe and luk2302.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:05/12/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<ios><objective-c><uitableview>', 'Title': "TableView: How to proceed after catching 'NSInternalInconsistencyException'?", 'ViewCount': 1389, 'id': 37186240, 'user_AboutMe': '', 'user_AccountId': 4123677.0, 'user_DisplayName': 'Farley', 'user_Reputation': 169.0, 'user_Views': 34.0, 'user_id': 3383372.0}}