In this blog, we will see how to show and delete the coredata. we’ll see how easy it is to get started with all the resources provided in Xcode, from starter code templates to the Data Model editor.
We will create a simple app to save all your information.
However, we only showed you how to insert records into the data store through Core Data API and left out the delete operation.
Let’s started:
Saving the CoreData
Note: If this is the first time you learn about Core Data, we recommend you to read the first blog on core data.
Firstly, Drag a tableview in the storyboard and connect it with ViewController.swift.
Open ViewController.swift, add the following Core Data module import below the UIkit import. This import is all you need to start using the Core Data API in your code.
1 |
import CoreData |
we’ll store employee entities rather than string names,
so you rename the array serving as the table view’s data model to Employee
1 |
var Employee: [NSManagedObject] = [] |
Employee represents a single object stored in core data. we will use it to create, edit, save and delete from your Core Data persistent store.
After that, still in ViewController.swift, add the following UITableViewDataSource extension below your class definition for ViewController:
1 2 3 4 5 6 7 8 9 |
extension ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Employee.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)-> UITableViewCell { let employee = Employee[indexPath.row] let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = employee.value(forKeyPath: "name") as? String return cell } } |
Saving a new object to core data is a two-step process: first, you insert a new managed object into a managed object context
Xcode has generated a managed object context as part of the new project’s template.
Note:- this only happens if you check the Use Core Data checkbox at the beginning. This default managed object context lives as a property of the NSPersistentContainer in the application delegate. To access it, you first get a reference to the app delegate.
Deleting an object
Let’s talk about the delete operation. To allow the user to delete a record from the table view.
As you know, we can simply implement the “canEditRowAtIndexPath” and “commitEditingStyle” methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { let context = managedObjectContext if editingStyle == .delete { // Delete object from database if let object = Employee[indexPath.row] as? NSManagedObject { context?.delete(object) } var error: Error? = nil if !(context?.save(error))! { if let error = error { print("Can't Delete! \(error) \(error.localizedDescription ?? "")") return } Employee.remove(at: indexPath.row) if let array = [indexPath] as? [IndexPath] { tableView.deleteRows(at: array, with: .fade) } } } |
For saving the data, first, we take manage object context that provides us a method called “delete object”.
To delete an object from the database we use the “delete object” function. we need to call the “save” method to commit the change. we also remove the record from the table view.
Lastly, use the comments area to share your thoughts with us.
For other blogs Please click here