1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Leveraging Core Data to Store Data on iOS

Retrieving Information

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
2h 10m
Description

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.

Transcript

Hi. Within this lecture, we're going to see how to retrieve information from core data so that we can display those in our table view. So, let's go to our view controller. Because that's what we're going to... That's where we are going to get this information because we want to display those information in our table view and I see we didn't even do the table view set up in here, but we can do that very easily because we have done that so many times. So, I'm going to do this very quick, I'm going to say this is UITableViewDelegate and UITableViewDataSource. So remember we have to do this TableViewDelegate and DataSource so that we can come over here to viewDidLoad and just say TableViewDelegate is self and TableViewDataSource again is self. I'm not explaining you the steps right now because we have already seen this many times. So, I said that you get to practice it many times. So, let's go for a number of rows and section which will define how many rows that is going to be displayed in our table view for right now I'm just going say 10 and cellforro at indexPath, it will define the individual cells inside of each row.

So, it expects us to create a UITableViewCell. So, I'm going to say UITableViewCell. And then in the cell I'm going to have a text label which displays the text of tests and I'm going to return this cell and here we go. How quick we did that in the first time we have learned this we spent 15 minutes now we can do this under a minute. So far so good. We have our table view ready for us. Now, it's time to actually get the information from the core data so that we can display those information in this table view. So, what we're going to get? We can get all those information like image, name, year, ID. But of course, it doesn't make sense to get all of those information and store it in this UIViewController. Why? Because we're going to display those details in the next UIViewController in the details UIViewcontroller. And we don't have to get a 1000 images in here. We don't have to download everything from the database. We're not actually downloading but we're reaching with inside of database. But again it would be very inefficient because we're going to need only one image. So, if I do that on the details ViewController, I can just say, "Okay, bring me the our selected paintings image so that it will take only one image out of the database and show it to me. If I do that over here I will just get 1000 images and show them, store them in variables. And it will be much more inefficient. So, I'm going to create a nameArray and I'm going to create an idArray, because that's the only two values that I will be needing in this lecture in this part of the app and again, nameArray will be string but idArray will be UUIDs. Remember we have created this as a type of UUID rather than string. So, we're going to get this as a UUID and we're going to save all this information as a UUID as well. So, rather than doing this under ViewDidLoad, I'm just going to create a new function because this will be a little bit long. So, we're going to write everything under this getData function.

So, how to get data from core data, database. It's actually very similar to the process of writing data. We need to get the appDelegate one more time. So, I'm going to write it. Of course you can copy and paste  this if you want but I believe the practice will make you better. So, I'm going to write everything from scratch. So, I'm going to say UIApplication dot shared and later on dot delegate. I'm going to first cast this as an appDelegate and then later on I'm going to create my context variable. So, this will drive from the appDelegate. So, let context be appDelegate dot persistentContainer like this container or persistentContainer, dot context. ViewContext. So, this is going to give us context and we're going to use this with our fetch method. So, fetching means getting information from the database and we're going to do that. In order to do that we're going to need something called NSFetch Request. Why do we need those? Let me say context dot fetch, it will ask me for to create an object called NSFetch Request with NSFetch Request Results. So, first I have to create this NSFetch Request so that I can give it to my context so that it will create a result back to me. And we're going to do that with do try catch one more time because it throws as you can see. So, let me show you how to create a fetch request. And in order to do that, I'm going to use NSFetch Request. And of course, if I cannot import this core data I cannot use anything. So, I'm going to go ahead and do that and I can just say NSFetch Request and inside of this NSFetch Request I have to have an NSFetch Request Result and I believe there should be something wrong with this because it should have asked me for an entity name and it didn't so let me delete this or if I open parentheses it doesn't still ask.

So, let me delete this add as I just write it from the beginning. So, as you can see it asks me for an entity name because we haven't provided our paintings entity name in anywhere. So, we have to do that first. If I leave it like this, it will still give me an error, I have to say NSFetch Request Results as you can see. So, it's kind of a bug. It should have just asked me everything at once. So this is an NSFetch Request Result and entity name is paintings. So that's how you create fetch request. As you can see Xcode actually guides us through this process. So, if you know you have to call fetch now you can just go over here and say context dot fetch and it will say I'm needing. I need something called NSFetch Request and if you try to create that variable, it will just ask you for an entity name and then request the result. Of course, it guides you but you have to know what you're going to do. So, that's what this course is about. Or you can just read the documentation but you have to be very efficient in Swift. Maybe like an advance in order to understand everything.

Later on I'm going to use something called returnsObjectsAsFaults and the reason that I'm doing that, I'm just going to set it false reason that I'm doing that. If this is false, it means that I'm not going to get the objects as false and it would be more efficient because it would just set up the cache and use, there's in the background of core data retrieving and writing and it would speed up the process if you're dealing with a lot of data.

So, we're going to have to say fetchRequest dot returnsObjectsAsFaults, false. And then later on I can just come over here and I can just call the context dot fetch as I did before, and again it will ask me for do try catch structure which is what we're going do anyway because once you go with core data, once you work with core data you have to get used to the fact that you have to use a lot of do try catch because it throws an error as you can see. So, the important part here is that it returns as an NSFetchRequest Results as expected but it returns me an array of these results, even if I have one object in the core data, it will still return an array for me. So, I have to cast this as a variable. I have to assign this to a variable and I have to use that variable to go inside of a for loop so that I can actually reach the individual and NSManaged objects. So, this is good but we have to do this in a do try catch as it suggests. And after the do try thing, we can just go in the for loop under do before we catch if there is any error.

So, it's fairly easy to do that, we have learned how to do it. So, come over here and say try, we're going to try this under a do code block. Let me cut everything in here and paste it. And then later on I'm going to catch if there's any error and I will print error for right now. And in here do try catch block. I'm going to assign this to a variable as I said before because why? Because the results will give me an array. So if I say array, I will see that this is any type. So, rather than giving me an NSManaged object, it gives me an any type. So, I have to cast this as an NSManaged object array. So, it's boring to cast everything and it's boring to do everything safe. I know, but this is how it works with core data unfortunately. So, I'm going to say halt for result in results and in order to cast results as an NSManaged object and I have to do that because I want to reach the NSManaged object type methods and functionalities in order to retrieve the data out of that object, I'm going to first cast this as an NSManaged object.

So, this is a core data model object and we can reach the functions of that core data model object so that we can say something like "Okay, bring me the value for key name. Bring me the value for key year." So, in order to do that, I have to cast this as an NSManaged object. So, if I say result, the individual result will be an NSManaged object as you can see. And now I can just say result dot value and it will give me a value for key name for example. And this will be again in any value, I have to cast this as a string optional string. And the best way to do it's actually use an if let one more time. So, I'm going to say if let name result dot value for key name as string. Then if this only this works out, then I'm going to append this on my nameArray. And if this doesn't work out, I'm just going to do nothing. So, if this works out, the only thing that is left to do for me is to append this to my nameArray. And we have created those arrays here so I can come over here, and say self dot nameArray dot append. And I will add the name value in here.

So, next what I'm going to do, I'm going to do the same thing for ID as well. So, I'm going to say if let ID dot result value for ID. As not string but UUID this time because we're going to append this as a UUID anyway. And then we're done. So, that's the two values that we have been looking for. So, the name and the ID. And in here after we are done, I have to come over here and refresh the table view. And before we do that, let me just say nameArray dot count here and nameArray index path row in here so that we will display the values for nameArray. And that's okay. But I have to also refresh the data because I'm adding something new to those arrays.

So, I have to go to the table view and say please refresh yourself for me. So, you can just go and say self dot table view dot reload data and it doesn't work because I misspelled the tableView. So, tableView that reload data. So, it reloads the data of the tableView. That's great. That's how we get information from core data. So, before we forget, let's call this function in viewDidload. So, we get the data once we open the application. So, let's try this. We only have one value inside of our core data. And here we go we see the waterfall. So, if I click on this, I cannot see the details yet. So, I'm not sure if my image is saved if my year is saved, but I can easily see the name is saved so I'm pretty certain that it works. And let me try to add another one. Let me choose this flower. And for the name, I'm going to go for flower. I'm going to just come up with an artist like Van Gogh, and the year will be 1900 and I'm just going to call save and it shows success but it should have taken back but it doesn't seem to be happening. We're going to have to manually do this. So, if I run this one more time as you can see, I see the flower. So, we have to fix those errors it has to take me back automatically and it has to show me the new value that I have saved. In order to do that, we're going have to implement some new codes. Let's stop here and do that within the next lecture.

 

About the Author
Students
205
Courses
55
Learning Paths
3

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.

Covered Topics