Database

Getting started with coredata in swift

Introduction
Getting started with coredata in swift

Core Data is one of the most famous frameworks supplied with the aid of Apple for iOS and macOS apps. Core records is used to control the mannequin layer object in our application. You can deal with Core Data as a framework to save, track, adjust and filter the statistics inside iOS apps, however, Core Data is now not a Database. Core Data is the use of SQLite as it’s continual shop however the framework itself is no longer the database. Core Data does a lot extra than databases like managing the object graphs, monitoring the modifications in the statistics and many extra things. In this quick post, we will see how to store and retrieve statistics the usage of Core Data frameworks.


Core Data Demo
In order to show fundamentals of the Core Data, let’s create single view iOS app with Core Data module selected.
Now that, we have created a demo mission with Core Data support. There are two high-quality adjustments in this Xcode template we can effortlessly take a look at which are
The new fileCoreDataDemo.xcdatamodeld
The AppDelegate.swift file with Core Data Stack code


Core Data Stack
The Core Data Stack code internal the AppDelegate.swift has clear documentation in shape of feedback however in short, it set up the persistentContainer and retailer the records if there are any changes. As AppDelegate is the first file that executes as quickly as app launched, we can keep and fetch the context the from the Core Data Stack.


Data Model
The new fileCoreDataDemo.xcdatamodeld acts as the mannequin layer for the records that we choose to save. We can without problems advert the entity, attributes and relationships from the UI as like any different relational database.
Now that, let’s say we desire to save the username, password and age attributes for the User entity. Select the CoreDataDemo.xcdatamodeld file and click on on “Add Entity” (+) button and title the entity as Users. From the add username, password and age as String kind attributes. The stop end result will seem like this 

Now that, we have modelled our information in the Users entity. It’s time to add some documents and shop it into the CoreData


Add Records to Core Data
The manner of including the information to Core Data has following tasks
1). Refer to persistentContainer
2). Create the context
3). Create an entity
4). Create new record
5). Set values for the information for every key
Let’s do the entirety interior theViewController.swift and viewDidLoad() method. As we comprehend that container is set up in the AppDelegates so we want to refer that container.

We need to create a context from this container.

Now let’s create an entity and new user records.

At last, we need to add some data to our newly created record for each keys using


Now we have set all the values. The subsequent step is to retailer them inner the Core Data
 

Save the Data
The strategies for saving the context already exist in the AppDelegate however we can explicitly add this code to store the context in the Database. Note that, we have to wrap this with do attempt and seize block.


At this stage, each time we run our app the new entries will be brought to the Core Data records.
 

Fetch from Core Data
The procedure of fetching the saved information from is very effortless as well. It has the following task
1). Prepare the request of kind NSFetchRequest for the entity
2). Fetch the end result from context in the shape of array of [NSManagedObject]
3). Iterate thru an array to get fee for the precise key
We can fetch the statistics from our Users entity the usage of following code.


 

Update Data
For replace file first we have to fetch/Retrieve records with predicate as identical as above Retrieve information process. Then under few steps to follow
1). Prepare the request with predicate for the entity (User in our example)
2). Fetch report and Set New price with key
3). And Last Save context identical as create data.


 

Delete Data
For delete report first we have to discover object which we favor to delete through fetchRequest. then observe beneath few steps for delete record
1). Prepare the request with predicate for the entity (User in our example)
2). Fetch file and which we choose to delete
3). And make context.delete(object) name (ref picture connected below)

Download Source Code:

Download

Share post on Social Media

0 Comments

Leave Replay