0

I have problem with reloading my tableview.Then I delete something on my tableview, the tableview should reload and the data should go up.But then I delete something nothing happens and there some blank spaces between the rows. I tried with tableview.deleterows but after I implemented this method my app crashes..

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), 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).

if editingStyle == UITableViewCellEditingStyle.delete {
            //let mt = movieArray2[indexPath.row]

            //1
            guard let appDelegate =
                UIApplication.shared.delegate as? AppDelegate else {
                    return
            }

            let managedContext =
                appDelegate.persistentContainer.viewContext
            managedContext.delete(self.movieArray2[indexPath.row])
            tableView.deleteRows(at: [indexPath], with: .automatic)


            do{
                try managedContext.save()
            } catch {

            }

 }      
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • @Sh_Khan: This is not a duplicate question, Please check again and also you already answer this question but now you have deleted and mark as a duplicate – Jogendar Choudhary Sep 18 '18 at 19:16

1 Answers1

1

You need to save managedContext before delete the cell like below:

do{
    let managedContext = appDelegate.persistentContainer.viewContext
    managedContext.delete(self.movieArray2[indexPath.row])
    try managedContext.save()
    self.movieArray2.remove(at:indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .automatic)
  } catch {

 }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26
  • 1
    All of the code should be in the `do` block. It logically groups together code that should go together as an atomic action. – rmaddy Sep 18 '18 at 18:38
  • @rmaddy: thank you, I have updated my answer – Jogendar Choudhary Sep 18 '18 at 18:39
  • I tried this but the app crashes again with the same error. – Ozan Gümüstas Sep 18 '18 at 18:45
  • @OzanGümüstas That's because you still need to update your local data model array in addition to Core Data. – rmaddy Sep 18 '18 at 18:46
  • How to update ? can u give me an example ? – Ozan Gümüstas Sep 18 '18 at 18:50
  • @OzanGümüstas: You have to update your local data array which is use for to reload the table means from which arrray you are getting data in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) – Jogendar Choudhary Sep 18 '18 at 18:54
  • @OzanGümüstas If you didn't get my point then show me your tableView(_ tableView: UITableView, numberOfRowsInSection method so in this method you are returing how many number of row you want so that array also need to update with your database – Jogendar Choudhary Sep 18 '18 at 18:56
  • @JogendarChoudhary override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return movieArray2.count } But moviearray2 is not an array its a Core Data object – Ozan Gümüstas Sep 18 '18 at 18:59
  • 1
    @OzanGümüstas: You need to update movieArray2 after save the managedContext – Jogendar Choudhary Sep 18 '18 at 19:00
  • 1
    @OzanGümüstas: You need to again fetch the data from coredata in movieArray2 after save the data – Jogendar Choudhary Sep 18 '18 at 19:02