Simran Preet Singh

I am a fullstack developer. If you give me one more task, my stack will overflow.
✍🏽 iOS | Python | SQL

Core Data Part 1 - Data Model | Simran Preet Singh

Core Data Part 1 - Data Model

April 03, 2024

Series

Introduction

In the first 3 articles in this core data series I will go briefly over the 3 main components of the Core Data Stack.

First we will just go over what is DataModel and how to use it in our iOS Project with CoreData

Data Model

Defined in the .xcdatamodeld file, this includes all the models that we want to have to store data in Core Data. For example if we want to have a Folders, Notes , Tags models for a notes app, than these will be defined in the .xcdatamodeld file.

.xcdatamodeld just holds the information about how the data will look like. Which means it just holds the classes for those models, the properties in those classes and the relationship between. In holds no actual data to be store. It is simply like a group folder named Models we create in XCode when we want to group our models classes or structs in a single place. But here these models are always classes as they need to be of reference type as other parts that we are gonna discuss later needs to track each reference and any changes done to them.

New iOS Project

To create a data model lets create a new XCode project called CoreDataExample

Make sure the storege is selected as None. We will create everything from scratch so we are not selecting any pre-existing type of storage. As that gives a lot of boilerplate code that can be confusing to many.

Now we get a very empty SwiftUI project that has no way to store data permanently.

Creating .xcdatamodeld in our project

First we will create a new folder group in our project named Models. Here we will keep the xcdatamodeld file and all the related files which defines the models for Core Data, their extensions if any and any other helper model files we need in the app.

Now lets create a new file in the Models folders and select Data Model under Core Data section in your file template window.

*** Now be very carefull here. Because the name that we give to this file is very important as we will use it later in the app to define other components of the Core Data stack. So name it in Pascal Case like a class and keep the name a bit simple to remember and find. I am naming it CoreDataExampleModel.xcdatamodeld



Creating our first Model/Entity in .xcdatamodeld

Now we have created our .xcdatamodeld file which will store all our data models related to Core Data. We will follow a naming convention here which I personally use. You are free to not follow it and use your naming convention, but then please make sure that you make necessary changes in this series of articles later on. What I do is usually suffix my Core Data models with Entity as it makes it easy to distinguish them from my other models in the project if I needs to create any later on.

So lets create our first model here named FolderEntity. Now open the CoreDataExampleModel.xcdatamodeld file and you will see something like this in your XCode.



Now you need to add an Entity which simply means you are creating a class which will define your data model. You create a new Entity by clicking the following icon in the xcdatamodeld file that you opened.



Now you see an entity as been created with the name Entity.



Just double click on it and rename to something else, in this case we will create an entity named FolderEntity.



Now we would need to add an attribute to this entity which means we are adding a property to our class FolderEntity. Click on the + icon in the Attributes section.



Now name the attribute to title and select its type(data type) to be String. We will explore other data types as well in upcoming articles but for right now we are keeping it simple here and using String.



Now just make sure your project builds fine. This whole process has create a class named FolderEntity with a property named title underneath the xcdatamodeld which is not directly visible to us. But if you projects builds trust me there is a class named FolderEntity that now exists in your project somewhere hidden from your eyes. We will later also learn to create those classes as Swift files in your project, but not right now.

In the next article we will explore what is NSPersistentContainer and how to create one for our project.