如果我在旋转设备时调用“insertRows”,UITableView 会崩溃 "Incorrect checksum for freed object"
UITableView is crashing if I call `insertRows` while rotating the device "Incorrect checksum for freed object"
我必须解析大量数据才能显示在我的 Tableview 中。
因此,我没有解析所有数据然后重新加载 table 视图,而是解析了 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).'
但有时我也会收到这个奇怪的错误,我不知道它是什么意思:
malloc:已释放对象的校验和不正确 0x1509c7000:可能在释放后被修改。损坏的值:0x71
尝试在闭包
中为indexPathsToAdd
使用捕获列表
func addRowsIfNeeded( indexPathsToAdd : [IndexPath]){
if indexPathsToAdd.count > 0 {
DispatchQueue.main.async {
[indexPathsToAdd] in
self.myTableView.beginUpdates()
self.myTableView.insertRows(at: indexPathsToAdd, with: .fade)
self.myTableView.endUpdates()
}
}
}
您的问题是您正在更新数据源数组 self.comments
但在插入 50 个附加评论之前不会插入行。大概是你的numberOfRowsInSection
returns self.comments.count
。
假设在开始插入行之前,数组有 100 个元素。
当您旋转设备时,它会导致 table 视图重新加载,调用 numberOfRowsInSection
- returned 的行数包括您添加到数组中的行,但您尚未为此调用 insertRows
。然后,这将成为 table 视图中行数的新计数(根据您的异常消息,这是 127)。
当你最终调用 insertRows
时,你会得到一个异常,因为 table 视图认为你应该有 127+50 = 177
行,但数组只包含 100+50 = 150
.
我建议您将新评论添加到临时数组,直到您准备好实际插入行。这样 numberOfRowsInSection
就会 return 正确的数字:
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()
}
我必须解析大量数据才能显示在我的 Tableview 中。
因此,我没有解析所有数据然后重新加载 table 视图,而是解析了 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).'
但有时我也会收到这个奇怪的错误,我不知道它是什么意思:
malloc:已释放对象的校验和不正确 0x1509c7000:可能在释放后被修改。损坏的值:0x71
尝试在闭包
中为indexPathsToAdd
使用捕获列表
func addRowsIfNeeded( indexPathsToAdd : [IndexPath]){
if indexPathsToAdd.count > 0 {
DispatchQueue.main.async {
[indexPathsToAdd] in
self.myTableView.beginUpdates()
self.myTableView.insertRows(at: indexPathsToAdd, with: .fade)
self.myTableView.endUpdates()
}
}
}
您的问题是您正在更新数据源数组 self.comments
但在插入 50 个附加评论之前不会插入行。大概是你的numberOfRowsInSection
returns self.comments.count
。
假设在开始插入行之前,数组有 100 个元素。
当您旋转设备时,它会导致 table 视图重新加载,调用 numberOfRowsInSection
- returned 的行数包括您添加到数组中的行,但您尚未为此调用 insertRows
。然后,这将成为 table 视图中行数的新计数(根据您的异常消息,这是 127)。
当你最终调用 insertRows
时,你会得到一个异常,因为 table 视图认为你应该有 127+50 = 177
行,但数组只包含 100+50 = 150
.
我建议您将新评论添加到临时数组,直到您准备好实际插入行。这样 numberOfRowsInSection
就会 return 正确的数字:
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()
}