using coredata to save tableview list not working

Ive setup a tableview controller and am using it for a weight tracking list w/ coredata to save all of the entries made. While everything works as far as adding entries and displaying them while the app is open, it's not displaying those entries on the next app load. So either my save or load method is not doing its job. I appreciate the help as im relatively new to swift coding.


import UIKit
import CoreData

class TodoViewController: UITableViewController {

  var items = [Items]()

  let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

  var selectedCategory: Items?{
  didSet{
  loadItems()
  }
  }

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

  //MARK: Table View Datasource Methods
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return items.count
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "Item", for: indexPath)
  let item = items[indexPath.row]
  cell.textLabel?.text = item.name
  cell.accessoryType = item.completed ? .checkmark : .none
  return cell
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  tableView.deselectRow(at: indexPath, animated: true)
  items[indexPath.row].completed = !items[indexPath.row].completed
  saveItems()
  }

  override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  return true
  }

  override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  if (editingStyle == .delete){
  let item = items[indexPath.row]
  items.remove(at: indexPath.row)
  context.delete(item)

  do{
  try context.save()
  }catch{
  print("Error deleting item with \(error)")
  }
  tableView.deleteRows(at: [indexPath], with: .automatic)
  }
  }

  @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
  var textField = UITextField()
  let alert = UIAlertController(title: "Add New Weight", message: "", preferredStyle: .alert)
  let action = UIAlertAction(title: "Save", style: .default) { (action) in

  let newItem = Items(context: self.context)
  newItem.name = textField.text!
  self.items.append(newItem)
  self.saveItems()
  }
  alert.addAction(action)
  alert.addTextField { (field) in
  textField = field
  textField.placeholder = "Add Weight"
  }
  present(alert, animated: true, completion: nil)
}
  func saveItems(){
  do{
  try context.save()
  }catch{
  print("Error Saving item with \(error)")
  }
  self.tableView.reloadData()
  }

  func loadItems(){
  let request: NSFetchRequest = Items.fetchRequest()

  do{
  items = try context.fetch(request)
  }catch{
  print("Error fetching data from context \(error)")
  }
  tableView.reloadData()
  }

}

Replies

You need to call loadItems() in your viewDidLoad.