enter image description hereAfter adding items doesn't show at simulator even the data added to DB, items should show in simulator with CoreData even it was showing perfectly before with standard plist using users default, kindly find below AppDelegate.swift file also ViewController.swift file
//
// AppDelegate.swift
// Destini
//
// Created by Philipp Muellauer on 01/09/2015.
// Copyright (c) 2015 London App Brewery. All rights reserved.
//
import UIKit
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
func applicationWillTerminate(_ application: UIApplication) {
self.saveContext()
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
//
// ViewController.swift
// Todoey
//
// Created by Philipp Muellauer on 02/12/2019.
// Copyright © 2019 App Brewery. All rights reserved.
//
import UIKit
import CoreData
class ToDoListViewController: UITableViewController {
var itemArray = [Item]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
//print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
loadItems()
}
//MARK: - Table DatatSource Methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
// cell.textLabel?.text = itemArray[indexPath.row].titile
//Ternary Operator
// Value = condtion ? valutIFTure: ValueIFFalse
cell.accessoryType = itemArray[indexPath.row].done ? .checkmark : .none
// if itemArray[indexPath.row].done == true {
// cell.accessoryType = .checkmark
// } else {
// cell.accessoryType = .none
// }
return cell
}
//MARK: - Table Vieww Delegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
itemArray[indexPath.row].done = !itemArray[indexPath.row].done
// context.delete(itemArray[indexPath.row])
// itemArray.remove(at: indexPath.row)
saveItem()
tableView.deselectRow(at: indexPath, animated: true)
}
//MARK: - Add New Item
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New Todoay Item", message: "", preferredStyle: .alert)
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "creat new item"
textField = alertTextField
}
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
//What will happen once the user clickes the add Item button on UIAlert
let newItem = Item(context: self.context)
newItem.title = textField.text!
newItem.done = false
self.itemArray.append(newItem)
print(newItem)
self.saveItem()
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
//MARK: - Model MOnpoulation Method
func saveItem() {
do {
try context.save()
}catch {
print("Error saveing econtext \(error)")
}
self.tableView.reloadData()
}
func loadItems(){
let request = Item.fetchRequest()
do {
itemArray = try context.fetch(request)
} catch {
print("Error fetch datat form loatItem() \(error)")
}
}
}