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

时间:2017-07-27 11:31:20

标签: ios swift crash

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

  

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搬出去。'

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()
            })
相关问题