Carl Hung
Carl Hung

Reputation: 555

tableView.deleteRows(at:with:) crashed every time

When I tried to remove an object of type Product that derived from NSManagedObject of Core Data. the object can be successfully removed. but it crashed on the line of tableView.deleteRows(at:with:).

So, every time it crashed, I open the app again, the object was removed successfully, but I just don't know why it crashed in tableView.deleteRows(at:with:).

How can I fix it?

class ProductListInSection {
    let sectionName: String
    var productsInSection: [Product]

    init?(sectionName: String, productInSection: [Product] ){
        guard !sectionName.isEmpty else {
            return nil
        }
        self.sectionName = sectionName
        self.productsInSection = productInSection
    }
}


var categorySections: [ProductListInSection]

// ...

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        let element = self.categorySections[indexPath.section].productsInSection[indexPath.row]
        AppDelegate.viewContext.delete(element)
        do {
            try AppDelegate.viewContext.save() // successfully removed.
        } catch {
            print("Fail: \(error)")
            return
        }
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) // crashed here.
    }
}

the following is the error message:

2017-04-21 15:54:42.159 POS[20241:2852960] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UITableView.m:1737

Upvotes: 1

Views: 1899

Answers (1)

Nirav D
Nirav D

Reputation: 72410

You forgot to remove object from array. Use remove(at:) on productsInSection and remove object from array then call deleteRows(at:with:).

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        let element = self.categorySections[indexPath.section].productsInSection[indexPath.row]
        AppDelegate.viewContext.delete(element)
        do {
            try AppDelegate.viewContext.save() // successfully removed.
        } catch {
            print("Fail: \(error)")
            return
        }
        //Remove the object from array
        self.categorySections[indexPath.section].productsInSection.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) // crashed here.
    }
}

Upvotes: 4

Related Questions