ios - 如果我在旋转设备 `insertRows` 时调用 "Incorrect checksum for freed object",UITableView 就会崩溃

标签 ios swift uitableview autolayout malloc

我必须解析大量数据才能在我的 Tableview 中显示。

因此,我最终没有解析所有数据然后重新加载 TableView ,而是解析 50 行数据并插入它们。

因此,用户可以立即看到一些内容,而我则继续解析其余数据并每次再插入 50 个数据。

如果我不旋转设备,这会完美运行,但如果在仍在添加数据时旋转设备,我的应用程序就会崩溃。

这是我添加数据并检查是否已添加 50 行数据:

self.comments.append(currentComment)

indexPathsToAdd.append(IndexPath(row: self.comments.count - 1, section: 1))

if self.comments.count % 50 == 0 {
    self.addRowsIfNeeded(indexPathsToAdd: indexPathsToAdd)
    indexPathsToAdd.removeAll()
}

以下是插入行的方法:

func addRowsIfNeeded( indexPathsToAdd : [IndexPath]){
        if indexPathsToAdd.count > 0 {
            DispatchQueue.main.async {
                self.myTableView.beginUpdates()
                self.myTableView.insertRows(at: indexPathsToAdd, with: .fade)
                self.myTableView.endUpdates()
            }
        }
    }

错误消息通常是:

*** Assertion failure in -[APPNAME.GodTableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.84.15/UITableView.m:2055 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (150) must be equal to the number of rows contained in that section before the update (127), plus or minus the number of rows inserted or deleted from that section (50 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

但有时我也会遇到这个奇怪的错误,我不知道这意味着什么:

ma​​lloc:已释放对象 0x1509c7000 的校验和不正确:可能在释放后被修改。损坏值:0x71

最佳答案

您的问题是,您正在更新数据源数组 self.comments,但在插入 50 条附加评论之前不会插入行。大概您的 numberOfRowsInSection 返回 self.comments.count

假设在开始插入行之前,数组有 100 个元素。

当您旋转设备时,它会导致 tableview 重新加载,调用 numberOfRowsInSection - 返回的行数包括您已添加到数组中但尚未调用的行插入行。然后,这将成为 TableView 中行数的新计数(从您的异常消息来看,这是 127)。

当你最终调用 insertRows 时,你会得到一个异常,因为 TableView 认为你应该有 127+50 = 177 行,但数组只包含 100 +50 = 150

我建议您将新注释添加到临时数组中,直到准备好实际插入行为止。这样 numberOfRowsInSection 将返回正确的数字:

var newComments = [Comment]()
var indexPathsToAdd = [IndexPath]()

// ...

newComments.append(currentComment)

indexPathsToAdd.append(IndexPath(row: self.comments.count + newComments.count - 1, section: 1))

if newComments.count % 50 == 0 {
    DispatchQueue.main.async {
        self.myTableView.beginUpdates()
        self.comments.append(contentsOf:newComments)
        self.myTableView.insertRows(at: indexPathsToAdd, with: .fade)
        self.myTableView.endUpdates()
    }
    newComments.removeAll()
    indexPathsToAdd.removeAll()
}

关于ios - 如果我在旋转设备 `insertRows` 时调用 "Incorrect checksum for freed object",UITableView 就会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56181116/

相关文章:

ios - 防止iOS时钟停止

ios - 在哪里可以找到 iOS8 蓝牙的文档

swift - 可选键字典

ios - 如何设置tableviewcell的自动布局约束

ios - UITableViewCell' 没有成员 'dequeueReusableCellWithIdentifier'

IOS Facebook 认证使用 node.js passport-facebook-token

ios - 如何在后台上传 PHAssets

ios - 如何使用iOS SDK授权和登录微信?

ios - fatal error : unexpectedly found nil while unwrapping an Optional value while retrieving default

ios - TableView分隔符消失