Koh
Koh

Reputation: 2897

Firebase + Swift Deleting Rows in TableView

Ok, I was following this tutorial (https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2) for deleting rows in tableView with Firebase but it just doesn't seem to work. My code as follows:

override func viewDidLoad() {
    super.viewDidLoad()
    configureDatabase() 
}

func configureDatabase() {
    self.ref = FIRDatabase.database().reference()
    if let user = FIRAuth.auth()?.currentUser {
        self.ref.child("mileage").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
            self.mileageDatabase.insert(snapshot, atIndex: 0)
            self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic)

        })


    } else {

    }

}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        let row = self.mileageDatabase[indexPath.row]
        row.ref.removeValue()
        self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: .Automatic)

    }
}

It throws the error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Upvotes: 1

Views: 1612

Answers (2)

Ivan Sinigaglia
Ivan Sinigaglia

Reputation: 1072

I think that here row.ref.removeValue() there is a mistake. You are mixing your Array with Firebase data. If you write just:

mileageDatabase.remove[indexPath.row] tableview.reloadData()

For me is working this way. You have to capture the userkey for that row. I have an array like yours (mileageDatabase = teste) but my variables are stored at an struct inside a sublcass (ChkList). That is the way i did it.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == .delete {

        let user: ChkList = teste[indexPath.row]
        let uk = user.key
        print("Userkey: \(uk)")
        ref.child(uk!).removeValue()
}
}

This way you will have removed the data from Firebase and the row form tableview. You can see my ask where i could figure out how to fix this at: Firebase: delete item from row (Swift 3)

Upvotes: 1

Sachin Vas
Sachin Vas

Reputation: 1767

The data source still hold that object whose cell you deleted . You have to remove that object from the data source, as you delete the cell.

Upvotes: 0

Related Questions