Farley
Farley

Reputation: 178

TableView: How to proceed after catching 'NSInternalInconsistencyException'?

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];
}

Upvotes: 2

Views: 1686

Answers (1)

Farley
Farley

Reputation: 178

@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.

Upvotes: 1

Related Questions