保存已编辑的核心数据实体不会被保存

时间:2019-01-28 01:14:05

标签: swift uitableview core-data tableview uicollectionviewcell

我有一个带有多个部分的TableView,使用Core Data框架填充了多个实体(称为“ Item”)。在cellForRowAt委托方法中,我调用一个回调,该回调在用户按下Enter键时调用。此回调应将他们刚刚在单元格的文本字段中键入的文本保存到核心数据模型,然后在其下方添加一个新的空单元格。

除了节为空并且您尝试将item.name设置为等于textfield.text时,所有其他方法均有效。如果我在该部分添加了多个项目,则它将保存我刚刚创建并添加到数据模型中的所有单元格,除了第一个单元格。

起初,我正在更新项目,然后保存上下文,但是会出现“错误:从上下文中删除托管对象后对其进行突变”的错误。参见下面的代码。

    /// Customize the contents of the cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: homeCellId, for: indexPath) as! HomeTableViewCell
    cell.delegate = self
    let item = items[indexPath.section][indexPath.row]

    cell.nameText.text = item.name

    /// Called when user hits Enter on the keyboard
    /// This allows them to enter a new item
    cell.addNewCell = { [weak self] newTitle in
        guard let `self` = self else {return} // Capture self

        if cell.nameText.text != "" {

            let newTitle: String = cell.nameText.text! // Grab the name øf the item the user just entered
            let itemToUpdate = self.items[indexPath.section][indexPath.row] // Grab the item they typed

            itemToUpdate.name = cell.nameText.text
            self.saveContext()


            // Create reference to indexpath below the existing cell
            let newIndexPath = IndexPath(row: indexPath.row+1, section: indexPath.section) // Set the indexPath to the cell below
            // Create new dummy item
            let newPlaceholderItem = self.coreDataManager.addItem(toCategory: self.categories[indexPath.section], withItemName: "")
            // Add dummy item to tableview array
            self.items[indexPath.section].append(newPlaceholderItem)
            self.tableView.insertRows(at: [newIndexPath], with: .automatic) // Insert it into the tableView
        } else {
            print("Can not add new cell since current cell is empty")
        }
    }

    return cell
}

经过大量的Google搜索,我发现上述错误的解决方案是使用如下所示的performBackgroundTask()。

/// Customize the contents of the cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: homeCellId, for: indexPath) as! HomeTableViewCell
    cell.delegate = self
    let item = items[indexPath.section][indexPath.row]

    cell.nameText.text = item.name

    /// Called when user hits Enter on the keyboard
    /// This allows them to enter a new item
    cell.addNewCell = { [weak self] newTitle in
        guard let `self` = self else {return} // Capture self


        if cell.nameText.text != "" {

            let newTitle: String = cell.nameText.text! // Grab the name øf the item the user just entered
            let itemToUpdate = self.items[indexPath.section][indexPath.row] // Grab the item they typed

            let container = (UIApplication.shared.delegate as! AppDelegate).persistentContainer // Grab the persistent container

            container.performBackgroundTask { (context) in
                // Make sure the object isn't null
                guard let queueSafeItem = context.object(with: itemToUpdate.objectID) as? Item else {
                    print("Error, could not update item.")
                    return
                }

                queueSafeItem.name = newTitle // Update the title of the item

                // Save the change that the user made to the cell he just edited
                do {
                    print("Saving context after modifying item")
                    try context.save()
                } catch {
                    print("Error saving when creating new cell: \(error)")
                }
            }

            // Create reference to indexpath below the existing cell
            let newIndexPath = IndexPath(row: indexPath.row+1, section: indexPath.section) // Set the indexPath to the cell below
            // Create new dummy item
            let newPlaceholderItem = self.coreDataManager.addItem(toCategory: self.categories[indexPath.section], withItemName: "")
            // Add dummy item to tableview array
            self.items[indexPath.section].append(newPlaceholderItem)
            self.tableView.insertRows(at: [newIndexPath], with: .automatic) // Insert it into the tableView
        } else {
            print("Can not add new cell since current cell is empty")
        }
    }

    return cell
}

1 个答案:

答案 0 :(得分:0)

经过一些调试后,似乎我没有正确保存“虚拟”单元格,不得不重新格式化一些代码。在Xcode或Swift的一端没有错误-只是通常的程序员错误。