Core Data

Updated 1 July 2020

Save

Hi guys, today we will learn about how to create a core data app in swift

Before starting it we need to know what is Core Data.

Core Data

Core Data is a set of Tools (referred to as a Framework) that will allow you to save (referred to as Persist) and Retrieve the data in your iOS application to the iOS Device that your application is running on.

It does this by implementing something called ORM (Object Relational Mapping). This basically just means that Core Data will interact with your Swift Objects without you having to worry about the code that is handled. Core Data will handle how the data from your Swift Objects is stored and retrieved from the persisted data stores like relational databases from SQLite or flat files.

Creating a Core Data App in Swift

1.) First, let’s create a new project and let’s select “Use Core Data”. Although you can add that framework for your existing project, it’s easier to add it from that place as everything will be wired up already for you.

Once the project is created, you will see a file like  CoreDataExample.xcdatamodeld  added.

When you click it, you will see a tool that allows you to configure entities which represents data models. You can define few things for each entity there but for us Attributes and Relationships will be most important.

2.) Let’s add a new entity called CoreData (to change it’s name once you’ve clicked Add Entity button, click the name of a newly added entity on top twice and type in your name).

Now, let’s add a few attributes that will define a schema of our new entity : name — String,  id — Integer 64

3.) Create a new object called managed object model,, persist in the storage and read it after that.

Note: CoreData uses a SQLite database as the persistent store.

 

 

4.) Now arrange the persist data in UITableView :

 

5.) To save data in your persistent store into the managed object context, you have to save it. Open ViewController.swift and add the following below  addData(_ sender: Any):

Key Points

Conclusion

So please follow the above step and and if you have any issue or suggestion you can leave your query/suggestion in the comment section I will try to solve that.

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