Blogs

CoreData using SwiftUI

Purpose of the article: SwiftUI is Latest Programming Language to create Apple Apps.

Intended Audience: iOS Developers

Tools and Technology: XCode, SwiftUI

Keywords: XCode, SwiftUI, CoreData

What is Core Data?

Core Data is a framework used to save, update, filter, track and delete the data within our application. We can use Core Data in iOS and macOS also. It is used to manage the model layer object within our application.

Creation of Core Data Project: 

Open XCode Project and create iOS Project and make Sure While Creating App Core Data must be unchecked.

Here we will create our core data manually.

After creating the Project folder structure will be as below.

Here we are creating a Core Data project using MVVM Architecture. Then comes our coding.

First, we should create a Core data model file.

How to Create a Data Model?

Click on your Project folder and click with two fingers on your touchpad. Then a menu will appear. Click on New File after that search data model.

After, click on Next and give the name to your data model.

After creating Data Model, the file will appear like this.

In this file, we must Add an entity that is below our file. Then give a suitable name for your entity. After adding Attributes and giving datatype.

Here, I have given the title and type as a string.

After that, we must create our core data manually.

In this file, we will create the Save data, delete data, get all data.

In the above picture, we are using NSPersistentContainer.

It will simplify the creation and management of the Core Data stack by handling the creation of managed object model, persistent store coordinator, and the managed object context. And we are initializing our data model file to NSPersistentContainer.

NSPersistentContainer. The NSManagedObjectContext object manages a collection of template objects, instances of the NSManagedObject class. An application can have multiple managed object contexts. A persistent storage coordinator supports each managed object context.

So, then we must come to our view model class. Here we are accessing the CoreDataManager methods to our View model file.

Our View model file looks like this.

In this file we are creating save(), getAllItems(), deleteTask().

Here we are using @Published. It was introduced in SwiftUI.

It is one of the most helpful property wrappers in SwiftUI, allowing us to create observable objects that automatically announce when changes occur. SwiftUI will automatically track these changes and claim ownership of the body of all data-driven views.

Below we created a struct in that we are assigning id. To update or delete the task by using id, we can delete or update the task.

Then we can come to our designing part.

In the above code, we are using TaskViewModel by using @StateObject.

It is the new property wrapper that initializes the instance of a class that Complies with the ObservableObject protocol and stores it in the internal memory of the SwiftUI framework. SwiftUI creates @StateObject only once for every container that declares it and keeps it outside the view lifecycle.

Coming to the design, here we are using VStack and HStack.

VStack is used for Vertical alignments.

HStack is used for Horizontal alignments.

Under VStack, we are giving HStack and List.

Here List is like TableView. In Swift, we are using TableView. But coming to SwiftUI, It is changed to List.

Under HStack, we are using two controls.

  1. TextField: we can enter our text. Here we are giving a placeholder in string format and assigning data to taskViewModel.title. we are giving a rounded style to TextField
  2. Button: it is used to save the data that we are entering in the text field. In this, we are giving the title is Save.

Then, come to List. In this List, we are using ForEach for assigning data.

For the delete option, we are using onDelete to ForEach. If we slide row, we’ll find the delete option.

Then our design will look like this without adding data.

Then our design will look like this after adding data.

In this way, we can create our core data App using SwiftUI.

I hope this article was helpful.

Leave A Comment

Related Post

Purpose to Contact :
Purpose to Contact :
Purpose to Contact :

Purpose to Contact :
Purpose to Contact :
Purpose to Contact :

Purpose to Contact :