Coredata:- Save And Delete data

Updated 31 August 2021

Save

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.

we’ll store employee entities rather than string names,

so you rename the array serving as the table view’s data model to Employee

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:

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.

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

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home