Core Data Part 1 Components of Core Data - Data Model
Tags: coredata, swift, ios, xcode
Published: 2024-04-08 00:56:28


## Introduction

In this series I will go briefly over the 3 main components of the Core Data Stack.
- Data Model
- NSPersistentContainer
- NSManagedObjectContext

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`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yngrbhut6ejldzhg9ylz.png)



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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v11hgyrsnaw7trwuij67.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7ry2ejuuz0kfgdiy9bx.png)



### 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t17na3wxrp7bo5whd55b.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8kkfvnyfkrkji88fx08.png)


*** 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`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqcc0b64uy2m11brapay.png)

<br /><br />

### 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r48rbhec2gzwmg2whuh5.png)


<br /><br />

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xn3zg1xfoovldrtxkbc.png)

<br /><br />


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvyhef8jd18p94ldxwue.png)


<br /><br />


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nmykcerrn39ldr4wc5x.png)


<br /><br />

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nu6ew75wvc9jylgvuz7v.png)


<br /><br />

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`.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opfscc74eum2hqb3l82u.png)


<br /><br />

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.