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 find a way so that when user taps one of the created art, it displays the details in the DetailsViewController, right? So, whenever a user chooses an art that he or she previously added, we have to display the details. And there are two ways to do that actually. We can create a new ViewController and we can display all the details in that ViewController. But also we can use these DetailsViewController as well. All we have to do is just a find a way to differentiate if user is trying to add a new art or he or she is displaying an previously existing art. So, we are trying to figure out if the user clicked on the TableView. So, if they click on the TableView, they're trying to display an existing record, right? Or if they clicked on the plus button, if they click on the add button. So, we have to come up with a way to differentiate between those. So, I'm going to come over my DetailsViewController and I'm going to create a new variable like a chosenPainting or chosenArt and it will be empty if they click on the add button, and if not, if they click on the TableView then it won't be empty. And you know how to do that, right? We can just set selectedPainting empty. If they click on the plus button and if they click on the TableView, then we're just going to take whatever they click on and display on the next ViewController. So, we're going to use prepare for segue, we're going to use didSelectItem() function as we did before. But if they click on the plus button, we're just going to make selected items be equal to an empty string. So, I'm going to call this chosenPainting and of course, I'm going to create a chosenPaintingId as well and this will be a UUID optional. Because I'm going to transfer the chosenPaintingId, because I'm going to take chosenPainting details like name, like year, like image from the Core Data using that ID. Of course, we can do that with name as well but it won't be efficient as ID because two paintings can have the same name but two paintings cannot have the same ID, okay? And under viewDidLoad, I'm going to just control if chosenPainting is not empty and if it's not empty, I'm going to retrieve data from Core Data. Else, if it's empty, actually, I don't need to do anything because it will be displayed as a default for me but for remembering things, for remembering how to proceed in the else, you can just add something like nameText.text is empty, yearText.text is empty. This will be default in this way, you don't have to do that. But just for safety precautions, I'm just going to do that as well. nameText.text is empty, artistText.text is empty, and the yearText.text is empty. So, if chosenPainting is empty, this all will be empty as well because it means that we're going to create another entry in the Core Data. But if it's not it means that we have to retrieve data because we have a chosenPainting from the TableView. So, we're going to do what we generally do when we try to pass data. We're going to create another variable in here, okay? Call the selectedPainting for example and selectedPaintingId. And ID again will be a UUID optional and we're going to make the selectedPainting and selectedPaintingId equal to the index path of the selected TableView row, okay? And then we're going to pass this information to the DetailsVC. Of course, we're going to use prepare for segue and didSelectItem in order to achieve this. So, I'm coming on the bottom of this and just write prepare for segue and didSelectRow, not Deselect but didSelectRow. So, under didSelectRow, what I want to do, I want to just do a segue as usual because eventually I will be landing on the DetailsView, right? So, I'm going to say performSegue and the identifier will be "toDetailsVC" and the sender will be nil. But in here, I have to make sure that under add button clicked, my selected ID, selectedPainting is actually empty. So, whenever a user clicks on the plus button, it will send selectedPainting as an empty string and in here, I'm going to make this equal to nameArray indexPath.row, and selectedPaintingId should be again getting from the idArray as well. Remember we're passing the data of the ID and in fact, I'm just passing this name data just to check if name data is empty or not. So, I'm going to be using ID in order to retrieve data from Core Data database. So, let's say selectedPaintingId idArray indexPath.row. So, this is now okay. All I have to do is just define the DetailsVC as a variable here and remember in order to do that, I'm just going to check if segue.identifier is the segue that I'm looking for, so this is toDetailsVC. If indeed this is the segue that I'm looking for, I'm just going to go ahead and create a destinationVC variable. So, destinationVC will be segue.destination. I'm going to force cast this as DetailsVC and then later on, I'm going to come over here and do a command B to synchronize everything and say destinationVC. it will give me chosenPainting and this will be equal to selectedPainting, okay? And destinationVC.chosenPaintingId will be now equal to selectedPaintingId. So, this is great, right? So, I believe we managed to pass the information to the DetailsVC and we knew how to do that. That's not even different from the things that we have done in the previous sections. So, if we come over to DetailsVC and if we get a chosenPainting, it means that we have to go to Core Data and retrieve those information. I will use the same thing as we did in the ViewController, but now I will just filter the results with the ID, with the help of the ID, okay? So, we're going to have to learn how to filter the results in the Core Data database because I'm just going to get the image, the name, the year and the artist of one record only, one ID only. I'm not going to just take all of the information from the database because it won't be efficient as much as we are doing this. So, let me show you how to convert this chosenPaintingId into a string. If you say chosenPaintingId.uuidString, it means that it's going to take your ID from here and convert it to a string. So, for right now I'm just going to print out this string UUID just to show you how does it look like, okay? So, so far we have been creating this UUIDs but we didn't use them. So, as you can see in the logs we see this. So, this is optional right now but it doesn't matter. We can just come over here and put an exclamation point here rather than question mark and it won't be optional. So, let me run this one more time, and you don't have to do that by the way, I'm just trying to show you what does a new UUID look like. And it looks like this. As you can see as I click for different items on the TableView, it displays different values. So, they don't have the same values. They have unique IDs and as far as I continue adding new information to my database, they will all have unique IDs. So, we're going to use those IDs and we're going to get information from the database filtering only these IDs. And it will be much more efficient than just downloading or getting every information from the database and then having to find out what we're looking for in all of those information. So, let's do that in the next lecture.
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.