This course focuses on a concept known as core data, which will allow us to store our data in a database. We'll then show you how to apply this concept to a real-life app which we will build called Art Book.
Learning Objectives
Learn how to create a local database allowing users to save data on their phones using core data
Intended Audience
This course is intended for beginners who want to learn how to build apps using Swift.
Prerequisites
To the most out of this course, you should have some basic understanding of programming and computer science in general.
Hi, within this lecture, we're going to go ahead and create our project in order to start building our art book. So, within this project, we're going to utilize Core Data. So, this lets us save our information into a local database. And let me show you how it's done. So, when you choose single view app, make sure that you choose this use Core Data checkbox. So far, we haven't been checking this app but in this lecture, in this app we're going to need Core Data so make sure this is checked. And for product name, I'm going to go for art book project and you can choose whatever you want. In this app, we're going to save the arts that our user has seen when she or he goes to a museum but of course, we can use this for any reason, any purpose that we want. So, make sure you create your app and save the folders and then we're good to go. So, let me try to make this into a full screen. So, when you first open this app, you will notice some differences. For example, we're going to have a different name, different kind of file that we haven't had before in our Xcode projects. And this will let us configure the Core Data setup so that we can start saving data and retrieving data in the way that we want. Remember this data is saved in the local database. So, this information is actually saved into the iPhone storage. So, whenever a user opens or closes the app or just opens and closes the phone, it doesn't matter. We can reach those information from the storage itself, okay? So, let me show you two different files in here. First, we have this ArtBook data model in here, okay? So,
if you click on this, it will open a page like that and you will see some entities and attributes and we're going to go to that later. So, let me show you the AppDelegate. So, this is again a little bit different than before. First of all, it imported the Core Data framework and in here we see two functions in which we can save the data to the Core Data database and we can retrieve the data from here as well. So, we're going to work with AppDelegate in this project as well. So far, we haven't been working on the AppDelegate at all. But right now, we're going to need this.
So, let me go back to this art model, ArtProject data model so that we can configure our setup. So, maybe you remember that we are saving the image, the name, the year, and the painter, the artist itself in our database, okay? So, we're going to choose whatever we want to save in here. We're going to create an entity. An entity is like a class. So, it defines the data model itself. So, we're going to have one entity and in this entity, we're going to have different attributes. So, if you go down, you will see some button called add entity and if you click on it, it will create the entity for you. Whenever you want, you can just click on this twice and you can change the name. For example, I'm going to call this Paintings. You can call this Art or whatever you want.
So, this is like a class as I said before, in which we have attributes, we have properties. For example, if you hit plus on here, you can add an attribute called "name" for example. And for type, of course, we're going to go with string because name will be a string. For example, the artist will be string as well. So, let's choose one that is not string. Let's go for a year for example. It's going to be an integer. And as you can see we have three integers 16, 32, and 64 bits. So, the higher the bit is the higher the capacity to store the number.
So, we're going to just sort the years like 1900, 1800. So we don't need actually integer 64, I'm going to go for integer 32. If we're going to work with huge numbers, you can go with integer 64 in a later project. But right now, integer 32 is good for me. So, let's add one more, we had image as you might remember, okay? And the image will not be a UI image this time because we don't see that type in here. Rather, we're going to go for data. We're going to convert our image to data and we're going to save it to the database as data. So, choose binary data from here.
So, what does it mean to convert an image to a data? We can actually do that in computer science. So, if you try to convert some image to data and if you just open that data with a text editor, you will see some gibberish characters, you cannot understand it. However, Swift will be able to convert the data to an image, an image to data so it won't be problem for you. To sum up this process, we will convert our image to a data and we will start the data in the database. And then we have the artist, the image, the name and the year and the final thing that we're going to add is an ID. So, this is not mandatory but it's actually a very good practice to have one.
So, what's an ID? This is a unique identification number or unique identification string, okay? And it's not going to be a number. It's not going to be a string at all. We're going to use ID object in here, UUID. So, I need to have an ID. I need something to differentiate my individual arts from each other. For example, let me try to come up with a way to delete my art, to delete my saved painting. How can I differentiate it? How can I understand which one to delete? It should have an ID. Maybe I can do it from name but I can actually save two paintings with same name, right? And I can actually save two paintings with same year and it's a high probability that I can come up with those and one artist can actually have more than one paintings. So, it's wise to have an ID in here, a unique identification number or unique identification value that differentiates our art from each other so that we can do this operation. And in here, you will see something called UUID. This stands for Universal Unique ID. And the good thing is that Swift will create and take care of this for us by itself, because we're not going to be doing this manually, right? If we are adding 1,000 arts in our own app then we will not have to calculate the IDs, we will not have to give manual IDs.
Swift will just generate a random string, a random text and it will assign it to be an ID. And it's not going to be a string, it's going to be a UUID type. Later on in the course, when I implement delete functionality and when I implement UUID, and show you the representation of the UUID, you will much better understand what I mean. But right now, know that we're going to leverage this AppDelegate, we're going to leverage the model that we have just created in order to save our data in the Core Data. So now, I'm going to stop here and within the next lecture, we're going to start by creating our user interface.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.