繁体   English   中英

TableView:捕获“NSInternalInconsistencyException”后如何继续?

[英]TableView: How to proceed after catching 'NSInternalInconsistencyException'?

我有一个带有UITableView的应用程序。 应用程序与服务器通信。

问题如下:

客户端 1 删除了 TableView 的一个单元格。 数据更新被传输到服务器,服务器将数据更新发送到客户端 2。同时,客户端 2 从 TableView 中删除一个单元格。 由于接收更新, NSInternalInconsistencyException被抛出,因为之前和之后的单元格数量不符合预期(差异是 2 而不是 1)。

该应用程序崩溃

[tableView endUpdates];

当然我可以用

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

但在成功捕获后,我得到了

[_UITableViewUpdateSupport dealloc] 中的 EXC_BAD_ACCESS 异常。

我该怎么做才能防止应用程序崩溃? 我必须在捕获块中做什么来“清理”情况? 重新加载 tableview 没有任何效果。

编辑:numberOfRows-method 看起来像这样:

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

    return [dataSourceArray count];
}

@synchronized(lock) 解决了我的问题。

我只需要确保只有一个线程能够同时操作数据源数组。 现在一切正常,不再有“NSInternalInconsistencyException”。

感谢 trojanfoe 和 luk2302。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM