How to create a login page with Core data in Swift 5.2
How to create a login page with Core data in Swift 5.2

How to create a login page with Core data in Swift 5.2– Part 1/2

Naveen VJ
AppleCommunity
Published in
6 min readJun 14, 2020

--

Core Data is an object graph and persistence framework that you use to manage the model layer objects in your application. It provides generalised and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organised by the relational entity–attribute model to be serialised into XML, binary, or SQLite stores.

What is object graph?

In an object-oriented program, groups of objects form a network through their relationships with each other — either through a direct reference to another object or through a chain of intermediate references. These groups of objects are referred to as object graphs. Object graphs may be small or large, simple or complex.

For example:

As we can see in the above image that a person mostly have apartment and an apartment may have a person who living in it. So they have a strong reference with each other by sharing their objects to their respective classes. This two objects create a simple object graph to pass the values with each other.

Core Data vs SQLite

iOS developers would have mostly gone through this search on Google. I have made mistake in my interview saying that Core data is a new feature implemented by Apple in order to replace the third party sqlite database and made a detailed explanation to make him understand about Core Data. He said are you sure ? but i was trying to convince him about core data. Eventually, I got rejected in my interview which made me think to check the questions which I answered. Found out to be a lot of difference between them.

  • Basically, Core Data is a framework used to save, track, modify and filter the data within iOS apps, however, Core Data is not a Database.
  • SQLite is a database itself like we have MS SQL Server.
  • Core Data is using SQLite as its persistent store but the framework itself is not the database.
  • But Core Data is an ORM (Object Relational Model) which creates a layer between the database and the UI.
  • It speeds-up the process of interaction as we don’t have to write queries, just work with the ORM and let ORM handles the backend.

So these are the major differences found out about Core Data. Let us get our hands dirty and start a new project.

Create a demo project using Core data

Create a project with Use Core Data

First, let’s create a new project and let’s select “Use Core Data” and “Use CloudKit”. CloudKit is a new feature introduced in Xcode 11 which we will see in my upcoming articles so don’t get confused about that. Once the project is created, you will see two changes inside the project .

  • CoreDataSample.xcdatamodeld
  • The AppDelegate.swift file will have Core Data Stack code inside the file
App delegate with core data boiler plate code

The Core Data Stack code inside AppDelegate.swift has a property called persistentContainer. It is used to assign data model file which we created earlier called CoreDataSample.xcdatamodeld . As AppDelegate is the first file that executes as soon as app launched, we can save and fetch the context the from the Core Data Stack.

CoreDataSample data model

Data Model

As we discussed earlier CoreDataSample.xcdatamodeld acts as the model layer for the data between the database and the UI. We can easily add the entity, attributes and relationships from the UI as like any other relational database.

Now that, let’s say we want to store the username, password and email attributes for the User entity. Select the CoreDataSample.xcdatamodeld file and click on Add Entity (+) button in the lower bottom left model screen and name the entity as Userdetails. It will show an entity named Userdetails click on the Add Attribute (+) button in the lower bottom right and add email, name and password.In simple terms Entity is like a class where attribute is like a variable inside our class. Now that , we have added all the properties inside our model let’s get into our project and do some magic.

Singleton class named CoreDataManager

CoreDataManager

As we seen earlier we have already created a persistent container inside appDelegate file but I would like to access all my Core Data methods under one class. So I have created a Singleton file class named CoreDataManager.

I have created a static object inside my class in order to create one instance for my class and again assigned a constant persistentContainer with my CoreDataSample data model.

Main tasks to be done inside our class are listed below:

  • Create new records
  • Fetch records
  • Update records
  • Delete records

Create new records

Let us start with creating a method with three arguments which we are going to save inside the data model. The methods consists of following properties

  • Context is used to get the attributes inside our model by assigning persistentContainer.viewContext.
  • NSEntityDescription.insertNewObject is used to insert the values inside our containerUpdate records
  • Userdetails is a NSManagedObject which has been created automatically inside our module when we created an entity.
  • Userdetails will have the following @NSManaged variables which we have created earlier in the attribute section also has been created automatically inside our code.
  • Final operation is to save the values inside our container by using context.save().

Fetch records

The process of fetching the saved data is very easy as well. It has the following task

  • Context is same as we discussed earlier
  • NSFetchRequest<Userdetails>(entityName: “Userdetails”) is used to fetch the all the values stored inside my NSManagedobject class
  • we can also use to filter the values by predicate to fetch the data.
  • Final operation is to fetch the values by using context.fetch(fetchRequest).

Update records

For update record first we have to fetch data with predicate as same as above Retrieve data process. Then end it by saving the context .

Delete records

For delete record we have to fetch data with predicate again to delete and end it with context.delete(userdetails) to end the process.

As you guys seen in the above operations where we didn’t use any complex queries or gone through a large process to setup a db inside our project. Well, this isn’t enough to save and fetch data. As you use more Core Data things get more complex.

Well, I hope you have learnt some basic operations to use inside our project. Let us meet in my next article to create a login page so stay tuned for Part 2 guys.

Reference

https://medium.com/xcblog/core-data-with-swift-4-for-beginners-1fc067cca707?source=bookmarks---------1------------------

https://medium.com/@ankurvekariya/core-data-crud-with-swift-4-2-for-beginners-40efe4e7d1cc

!!! KEEP LEARNING !!!

Thank you for reading, please hit the recommend icon if like this collection 😊 . Questions? Leave them in the comment.

You can catch me at:
Linkedin: Naveen vj

--

--