[MUSIC] Okay, let's put the code together now to connect our interface to our managed object context. So that we can allow our user to create some data, then we can persist it for them. All right? To do that, we're gonna need to make the interface, which is going to collect the data. Then, we're going to also have a way to list the data in our interface. We will add a method to delete the last entry in our interface with another button. And then, we will write code to add an entity as well. Write the code to show our data, and then write the code to delete the entity. So, to connect it all together, all right? So, let's go back to our project. [COUGH] Excuse me. Let's go to our story board and let's set it up for ourselves. Turning off size classes. And then, I'm gonna add a text field where we're going to enter in the chores that we'd like to do. Go about halfway out, because we're gonna be building it out for the rest of the interface soon. We'll add a button, add a button below it. And we'll make it the same width. And we'll say, make that button say Add A Chore. All right, and then we're gonna make a label and that label is just gonna be someplace where we can show our persistent data. Now there are gonna be better ways of linking our persistent data to user element items that we'll look at in the future, but for now, this will work. All right. And I wanna set some properties of that. For example, I would like it to be centered and I would like it to display more than one line. Let's just say 50 lines and we'll scroll, I guess, if we don't like it. Then, I want the label to be constrained to the bottom. So, we'll constrain the label to the bottom. And we will take this element and we will say it is going to be that distance from the top. And this element will be [SOUND] Did I get that right? And this element will be centered horizontally and have the same vertical spacing. Okay. Now lets see the interface that we have available to us with our View Assistant. And what we're going to do is we're going to have a chore tapped function that we're gonna add here. All right, what our chore tapped function is going to do is it's going to be when we type Add a Chore, we're going to use our app delegate, the app delegate that we initialized when we first loaded our interface controller, and we're going to ask our app delegate to create our chore managed object and return it. Now, we're getting an error there, presumably because it doesn't know about the chore managed object, which is strange because we have already included our AppDelegate.h. But when we created our chore, when we created our createChoreMO method, in our AppDelegate.m, we didn't put a interface, a declaration in the .h file so that other functions could use it, would know about it, so the Xcode would know about it. So, we come back here to appDelegate.m. These are the three functions that we added, three methods we added. I'm gonna copy them. And then, I'm gonna go to our header file for the app delegate. We can see that some other things are already here. These were created by Xcode primarily. Let's get us a little more space. [NOISE] Can we please These are the things that were added by Xcode. Let's go ahead and add our functions that we had that we want the rest of the world to know about. Actually I kind of like to put them below the properties. That is just a style item. All right, now these are the actual code. All we wanna tell people about are the interface. We don't wanna tell them about the implementation. [NOISE] So, by putting these declarations into our .h file, anyone who includes this .h file will be able to use these functions. Now within this .h file, we still don't know about these objects, so we also need to import ChoreMO.h in this file, and import PersonMO.h in this file, and import ChoreLog.h in this file. If all goes well, ChoreLogMO.h. If all goes well, everything will compile okay. Great. And now, we can go back to our storyboard and bring up our assistant again. And we can ask, where did our interface go? Hm, that's kind of weird. Okay I'm not sure why our interface disappeared there, but I'll rebuild it, and we'll go from there. All right. So let's run it, see if our layout looks okay. All right. While the simulator is coming up, we'll come over here to choreTapped. There it is. Okay. Great, that looks fine. All right, so now when we come over to choreTapped and our interface is back in place, we can see that we have this variable c. We've called create chore managed objects, so c is going to become a managed object that we're gonna be able to work with. And that managed object, we need to have its name. So, we're gonna need to get the name for the chore, from whatever the user entered in. So, we want to get that here. Get that here. And we'll call that Chore Field. So, when the user enters in the name for the chore we're gonna add it. So, we need to get that text. And now we have this managed object. And this managed object, remember, was created from our entity. And remember, we had this thing, this element called chore name, which we set up in our managed and our entity builder. So now, we can just get the chore field text and assign it to that managed object. Now this managed object, there's a reference to it in our scratch pad. So, as we write it, it's being managed for us by our managed object context. So, after we set the chore name of that object to what we want it to be based on what the user entered, we're going to go back and we're gonna get a reference to our appDelegate, and then we're gonna call saveContext. And that's gonna make all of our managed objects that we're working with, wherever they are in their application, they're gonna be persisted. They're gonna be saved to disk. So after I run this line, any change that I need to see will be there when we restart our program. If the program were to crash for some reason in the millisecond between the time when we set the chore name, and before we saved it, that chore name would not be remembered. That would not have been saved. In fact this object that we created would not have been saved even as an empty object. Then the last thing that we're going to to is we're going to update all of the names of chores that we're gonna put here down here in our label. So what does that look like? Well let's see. Let's save everything so we don't lose it. And what I'd like to do is just get a little bit more space here. Here we go, okay. So what Update Log List is gonna do is fill out our label at the bottom for us. So again, we're going to get our managed object context, because what we wanna do is we wanna get all of the chores that are available to us and put them in the bottom. So, in order to do that, we have to have an iterator that's going to go through every element in our database so that we can do something with it. In this case, what we're gonna do is, we're gonna pull out the name and we're gonna add it to our label text. So, we're going to have a fetch request. We're gonna fetch. What are we gonna fetch? Well, we're gonna fetch all the elements of our chore entity that is referenced from our entity builder. And then, we have some code here where we're going to get the results, we're going to execute that fetch in our managed object context. And as long as we have some results, as long as we receive some results back, if we didn't, there's an error. We'll abort our program which is bad for production code, you don't want to do that in production code. Instead you'd want to do something more graceful, but for teaching it's okay. And then what we're going to do is we're going to create a mutable string that's going to be initialized with nothing. And we're going to go through each element in our persistent data, and we're going to add the name of the chore to our string. So we're gonna be doing that through a fast enumeration method, going through each of our results, where each element in our results is a chore managed object that we've gotten from our persistent storage. We're going to append to our tour-managed object a new line character in the name of the chore from that particular persisted object. And then when we're done, we're going to save that in the label, which we'll call persisted data. But we haven't called it that yet, so let's come back here to our storyboard. Great, it hasn't gone away, happy for that. And lets give our label a name. Our label's going to be called persisted data because it's going to show the data that has persisted. Very good. Let's go ahead and put a constraint on it so that it's tall enough so that we can see. The results. So, when we tap the chore, we're gonna create a new object. We're gonna set the name of the chore to the name that the user has entered. We're going to tell our manageObject context to save everything. And then we're gonna update our log list based on what has been saved. And since we would like our application to start out with the newest data that's from, and that's been persistent, when we first start our application, We're also going to update our log list, so that when we start our application everything that's in persistent storage will be there. I'm gonna delete this template code here for clarity. The last thing that we'd need to do is we didn't actually connect our button to that, so we're gonna connect our button to that action. I need to do it with the right Here we go. No, the other way. Okay. Now, if we run our program, we should be able to add chores to our list. We should be able to stop our program and that data should be persisted. And that's an example of securely storing data using core data. Let's see if it works. All right, here's our application. Our label down here is empty because we don't have anything in persistent storage. If we would like to add a chore we can add washing dishes. We'll add a chore which will ask for a new persistent, a new managed object. We'll set the name of it. We'll save that persistent, we'll save all the persisted objects. And then we'll update our list. Yay, it's there. And then let's see, let's do wash the dog. We'll add that chore. Same thing happened. Now, if we stop our application, and we actually swap it out of memory. And then we run it again. We'll see that our chores have persisted across multiple runs. All right, so the only thing that we have left to do is to write the code to delete an entity. To do that, we do the same thing. We access our managed object context. We find the entity that we want to delete, and then we can execute a deletion. So let's do that. Let's go to our interface, stop the running application. And let's just add a delete button. And we'll add a function. That is deleteTapped. And in this case, we have to identify what is the element that we want to delete And to do that, we're just going to do very similar. Request. We'll just request everything and we'll delete the first one. No, we'll delete them all. How's that? That's good. [SOUND] So we're going to here lets get a single view again. All right. We're gonna get our manage object context that we've stored from our app delegate when we started our view controller. We're going to request. We're gonna create a request, a fetch request for all of the elements and chore. And we're gonna execute that request and as long as we don't have errors, we're gonna have this result object. And then we're gonna go through each one of these. I'll copy it from down here. And rather than printing something out or rather than collecting data, what we're gonna do is we're going to send a message to our managed object context to delete an object. And the object we're going to delete is going to be C. And then, the last thing we'll do after we do this, is we'll update our list again. So, by pressing that button. We will go through and we will delete all the chores and in order to make them persist, we also need to make sure that we, after that that we reflect those changes in our persistent storage. So we'll have to do, save context. So that, and the scratch pad, or managed object contacts, knows that those elements have been deleted. But it's our scratch pad, in order for our scratch pad status to become persistent, we have to say, save contacts. Whether that involves making a new object, or deleting objects. Okay, so that should be good. So last thing we need to do, is we need to wire it to our delete button. And now if we run, we'll see those elements before that were there when we ran. And if we hit Deete, they're all gone. Now, let's add some more chores. And then what I'm going to do is, I'm gonna exit this. And I wanna point out that if you completely remove an app from your phone, and you get this warning that says, delete all the data as well, what that means now, and maybe it's a little bit more clear, is it's gonna delete the SGLite database that's associated with your persistent object. So if we actually delete the app entirely and then we rerun it. We'll see that in this case the data didn't persist, that's because we didn't just stop the application, we actually removed it entirely from our phone. And when we remove it entirely from our phone, that assumes the users uninstalling the app and wants the space back that it was using to store data. Okay. So in summary, core data has a special set of tools for creating models. We've seen that. And once built Xcode will write the code for us to manage the models and to persist the data. But we have to wire the interface in order to create a new object, in order to put it into the database and in order to delete it if that's what we wanna do. Thank you. [MUSIC]