Home > Net >  How to Save deleted rows after closing application from background?
How to Save deleted rows after closing application from background?

Time:08-25

I'm trying to create A TODO List app .. I'm saving user inputs in UserDefaults and its working while I'm closing the app from home button. but when I remove the app from the background apps all what I deleted previously come back again here's some codes : code for delete row from tableView

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        items.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

code to edit on GUI

DispatchQueue.main.async {
    self.items.append(text)
    var currentEntry = UserDefaults.standard.stringArray(forKey: "items") ?? []
    currentEntry.append(text)
    UserDefaults.standard.setValue(currentEntry, forKey: "items")
    self.tableView.reloadData()
}

CodePudding user response:

You save the items string array to UserDefaults when you append something, but when you delete an entry you are not saving it to UserDefaults.

Your code works for me as expected, if just you add

UserDefaults.standard.setValue(items, forKey: "items")

after removing the item from the items array.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        items.remove(at: indexPath.row)
        UserDefaults.standard.setValue(items, forKey: "items")
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

ALTERNATIVE

You add a didSet to your array that saves the values to UserDefaults whenever it get changed, so you don't have to call that every time:

var items  = UserDefaults.standard.stringArray(forKey: "items") ?? [] {
        didSet {
            UserDefaults.standard.set(items, forKey: "items")
        }
    }

Then you can do your code to delete rows:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        items.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

And it is also not necessary anymore to save it manually when you add an item:

DispatchQueue.main.async {
    self.items.append(text)
    var currentEntry = UserDefaults.standard.stringArray(forKey: "items") ?? []
    currentEntry.append(text)
    self.tableView.reloadData()
}
  • Related