Dispatch.main.asyncAfter() 在完成之前调用时崩溃应用程序



所以我有一个带有单选按钮的应用程序来检查任务,只要我让动画完成,一切都会按预期工作,但是如果我同时按下两个按钮,我会收到错误:

libc++abi.dylib:以 NSException 类型的未捕获异常终止

每次点击单选按钮时执行的代码

func RadioTapped(_ cell: TableViewCell) {

if let indexPath = tableView.indexPath(for: cell) {
// Removes task from coreData
let task = self.tasks[indexPath.row]
print(tasks.count)
self.context.delete(task)
print(tasks.count)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do{
self.tasks = try self.context.fetch(TodayTask.fetchRequest())
// Animates the removal of task cell
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(800),execute: {
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .fade)
self.tableView.endUpdates()
})
} catch {
print("Fetching Failed")
}
}

}

错误信息

由于未捕获的异常"NSInternalInconsistencyException"而终止应用,原因:"更新无效:第 0 节中的行数无效。 更新后现有节中包含的行数 (11( 必须等于更新前该节中包含的行数 (13(,加上或减去从该节插入或删除的行数(0 插入,1 删除(,加上或减去移入或移出该节的行数(0 移入, 0 搬出(。

表行删除操作还需要删除表数组的元素。

根据您的代码,self.tasks可能是表列表的数组。

从要删除其行的相同索引中删除"self.tasks"的元素。

提示:

// Remove array element
self.tasks.remove(at: <index.row>)

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(800),execute: {
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .fade)
self.tableView.endUpdates()
})

相关内容

最新更新