[MUSIC] Okay, we're continuing in our case study. Up till now, we've created some subclasses of our different view controllers. We've laid out the user interface. And we've provided the mechanism by which our managed object context can be passed down from view controller to view controller, through the hierarchy. We created a subclass of our table view cell. Not the table view controller, but just the table view cell. That's where we're going to fill in the labels that show what the title and the due date of our elements of our core data entities are. We had to use a date format in order to convert our date into a string that we could format in a way we wanted to. So now we have to do the piece that has the most boiler plate code that we've looked at before. Implement delegates for our table view. For our table view, and we need to implement a delegate for our fetched results controller, so that we can connect our table view to the core data omen. And we've done this before and there's a lot of boiler plate codes. So like any good television chef, I've prepared some of it in advance so that we don't have to walk through it all in horrendous detail. But we still will go through it line by line so you can understand what's going on. And then we're going to implement our handles to do entity. So that when we segue from our table view controller to our entity, we can pass the to-do entity we're working with. Down to our detail so that we can edit that core data element in the form. All right, so here we are, this is our to do list. And the first thing that we're going to do, is we are going to implement our delegate functions. Now, one question you can ask yourself is where do you want the delegate to be? And we're going to go ahead and make the delegate of our table view be the same class. So we could have it be a different class, but we won't do that. So let's come here and let's take a look at our table view. And let's just right click on it here, and see if we have our delegate specified. We do have delegate specified and if we spread it out, we can see that the to-do list class are delegate. And our to-do list element here is our scene that is implementing it and our class for our to-do list is my UI table controller. So that's a place we should put our delegate functions. We could change it if we wanted, but that's fine. It makes sense to have the delegate B the class that's drawing it as well. So my UI table view controller over here. All right, our header file is pretty straight forward. This is handling the mock. Within our UI though what we're going to need to do is we're going to need to implement some of these interfaces. So that we can get this to work. So let's start by implementing our NS Fetched Results Control Delegate. Now if you'll recall, what this is is this is like the query that fills up our table. It doesn't connect the results of the query to the, doesn't connect the results of query on our core data to the UI. But it is the query itself. And that's all part of core data. So let's go ahead and import core data .h. I used the angled brackets because it's a system library. And maybe it shouldn't be. How's that? Okay, so the protocol call, NS Fetched Results Controller Delegate is now known because I included the file in which that protocol was defined. But now, I need to do the work that's required for creating that, creating that query. And that query is going to be housed in something called our results controller. So we're going to go ahead and create a property, which is an NSFetchedResultsController pointer. And we're going to call it resultsController. When we start our, when this view controller loads, we're going to initialize that results control. And we're going to initialize it down here in a function called Initialize Enest Fetched results controller delegate. That goes down to this point here. And it's not going to get called by itself, we have to call it. So we're going to come up here and after our viewDidLoad, we're going to call selfInitializeNSTouchControllerDelegate. And this is going to query our data object and give us something that's live. These comments here were provided by the template code and they're not relevant for what we're doing, so I'm going to delete them. [COUGH] And now I'm going to go back down to where we create our delegate function. Here we go, so the first thing that we need to do is we need to build up that results controller. And so we're going to do the things that we do standard, which is to create a fetch request. And we're going to allocate it and anent it, as we do with any object that's managed by arc. We're getting a warning because we're not using it anywhere. But we haven't un-commented the code yet. So the first thing we're going to do is we're going to say what is the entity over which we want this fetch to operate? What's the table from which we want to draw results. We come back to our core data model. We see that ToDoEntity is the name of our entities that we're working with. So that's what we want to work with. So ToDoEntity has entered as a string. We are using the managed object context that we have received already in the life cycles. In the life cycle of this Table View Controller. Remember when our application loads are AppDelegate passes the Managed Object Controller try Navigation View Controller. Our Navigation View Controller immediately passes it to our Table View Controller, and then our Table View Controller have it ready to go. When the view gets initialized. So our manage object content is ready so the thing we're searching over is our to do entities. And the grouping that we want, there's no grouping so it's just going to be everything. The true predicate is constant that we use to say we want no grouping. But if we want to do sections where we would define sections and the way we want to sort it is, I don't want to sort by due date initially. Initially, I just want to sort it by to do title. And that's gotta match our element and our core data model, so to do title is what I'm matching right there. Okay, and I want it to be ascending. So basically it's going to be alphabetical by title. All right, once I have the description of my query as represented in the fetch request, then I can create my results controller by allocating a new one. And initializing it which the fetch with the fetch request that I just declared. And using the Managed Object Context which I have a copy of locally and not worrying about the section named key path or the cache name. [SOUND] And lastly what I say is, whenever there are changes in our query. Whenever the underlying database changes, I want to be told and that is a delicate pattern. And so I need to set the delegate of the results controller to be this object itself. Now I can only do that because I have implemented the protocol NSFetchedResultsControlerDelegate. Because of that I'm allowed to set this delegate equal to myself. If I didn't do that, even though I haven't. Here, we can show what happens if we do that. If we comment it out, we get a warning because this function, this class that I'm operating in doesn't implement the protocol that's required to assign it to the delegate. So we'll go ahead and declare that we implement that protocol. And then the last thing that we're going to do is we're just going to launch that query for the first time. So we're going to say, okay results controller that we just created. Start doing your fetch and monitoring the database for us. All right. So that's what we needed to do in order to get out underlying controller generating data and giving us callbacks. And now we need to come up here and we need to implement these other two protocols. And these are the ones that we use to connect the UI itself, the visualization of our results controller. So I'll add those up there and clean up the code a little bit. And this is where we specify how we're going to draw our table view. So for starters, the number of sections in our table views gotta be one. We don't have any groups, so it's always going to be one big group. And the number of rows in that section is going to be equal to the number of objects that are coming back from our query. So the number of rows in our table view equal to the number of elements in our core data Database. That's pretty straightforward without groups. And then the last piece here is going to connect how are we going to draw the contents of our table view cells, when it comes time to do so? So when our table view is asked to draw a cell what are we going to do? Well, we're being asked to draw a cell on this table view. We're looking at an index path which contains the group and the row. And we're going to create, we're going to get the item from our core data which should be drawn at this cell. And so we see here that we have a warning, that's because this code isn't aware of our ToDoEntity. So I'm going to go the top. And I'm going to import our code, ToDoEntity.h, so that it is aware of our code. And so I'm going to ask my results controller from query give me the item at section whatever section and at row whatever row. Section better be zero. And row can be anything from zero to max minus one. Then I'm going to ask my table view to give me a copy of the prototype cells. Now if if we got back to our story board, and we go to our to do list scene, we come down here to our table cell identifier, and we come over here and we see this where we see this is where we said established our identifier. So I'm going to copy that. And this is the name of our prototype cell. I'm going to come back here and I'm going to say, this is the prototype cell I want. Table cell identifier, we're going to get a cell back and the type is going to be MyUITableViewCell. MyUITableViewCell, that looks good. My UIT will be cell that looks good as well. I'm getting an error for some reason, I'm not sure what's happening. See, See if it'll tell us, missing Bracket, start [INAUDIBLE] expression. For a second. Live coding, it's hard for me to see what I'm doing wrong here. Let's start by just trying to pass that signal to the receiver, that's okay. All right, let's see if we can now cast it to MyUITableViewCell. No, it doesn't know MyUITableViewCell. Why would it not know that? I am in my table view controller. Okay, I feel like, oops. Let's go back here. There we go. Right, I just hadn't allowed the compiler to know about that code. Okay, so, I'm going to get the item from core data that corresponds to the row that I am drawing. I am going to create a cell that is drawn from my pool of cells, from the prototype pool. And then in my cell I am going to set all the fields. But remember, this is when I created my subclass in my view cell, I created this function called SetInternalFields. So it's going to take my core data element. And it's going to fill out all of the elements of my UI in that row as appropriate, and it's also going to keep a copy of the entity that I'm using. So it's function's called setInternalFields, and the todoEntity is item, I am sending it. And I don't need any of this stuff, that's not part of the signature. Good, all right, and I think that is everything for implementing those two, those three protocols. We don't have any warnings, that's great. We have enough information now that we can draw the appropriate table. And we have information about changing. We have information about getting the results controller. And the last thing that we need to do is just implement the boiler plate code for updating this row. Animating the rows when we have a change in the underlying core data. So that is going to be pretty straightforward, so we get signal from our results controller that says that the controller is going to change content. And so we start an animation transaction, we saw we're going to do a bunch of animation, so wait. Then when an object did change we're going to see what kind of change it was. And if it was an insert then we're going to insert that new row in the right place. If it was a delete then we're going to delete the row at the right place. And if it was something that changed then we're just going to change it, but we have to use the right Signature, we're going to change it to the item that we received. And lastly, if it's a move we're going to delete the previous one. And we're going to insert the next one. And then finally, when we're done, we're going to end the animation. Again, this is almost just boiler plate code. All right, so we go back to what we intended to do. We intended to implement these delegates, these protocols. And the last one we wanted to do is DPHandlesToDoEntity. And that was one that we created, a protocol. And so the reason why we wanted to do this, is because when. Let's go back to our storyboard, [SOUND] when we have a list of elements here, and we transition to our ToDo pane. We want to pass our ToDo pane, a core data ToDo entity. So that it can fill out these fields with whatever the current entity is so that the user can edit them. So how is this view controller going to have access to that core data element? Well, it's going to have to receive it in the same way that it received a managed object context. So I want to implement that before we're done with this chunk of our case study. So that's why we implemented this protocol DPHandlesToDoEntity and so let's go and look at that. All right, copy what the function that we need to handle. And let's go to our view controller. And let's say, not only can we receive a managed object context, but we can also Implement the DP handles ToDoEntity protocol. Our compiler doesn't know about it so we have to import it. And then we have to actually declare that we implemented this particular function. Let it settle out, and we have an error. What's our error? Expected a type, do we not know about ToDoEntityType? All right, if that's the problem then this is a dumb place to put it. If you're going to implement this then you should know about it. So we're going to go ahead and import it there. Instead, so we will have a cascading import if all goes well. We go back to our view controller. Great, so when we import this HandlesToDoEntity, we subsequently import the ToDoEntity itself. Shouldn't have to implement both of those. All right, so then the last thing that we're going to do is we're going to come here. And we're going to come down to the bottom. And we're going to implement that function. Copied the wrong thing. We're going to implement that function by. [NOISE] We haven't declared a local to do entity yet, but we're going to set it equal to the incoming to do entity. Then up here, we will have our local LocalToDoEntity. And this is going to be that core data element which we are editing in this view controller. We have no warnings, we have no errors and so we're going to stop it. And that's everything we needed to implement in this section. Very good, so it should run okay without any errors. There's no information in our database so we're not displaying anything. And we haven't added any capability to save new data to our Database, so we shouldn't have anything show up. Okay, so we don't have any errors. We got this warning, this some information here that we might want to take a look at. But for the time being, we're good to go. All right, so that's the end of this video lecture. The next thing we're going to do is we're going to take care of the actual process of editing our ToDoEntities. We're going to connect our UI to the changes in our core data underline. And that should be, I think, the last thing that we need to do except for maybe a little bit of clean up elements here and there. All right thanks for watching. Almost. [MUSIC]