Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Swift
  • Overview
  • Transcript

5.5 Deleting Feeds

Okay, so let's start to add some nice delete functionality to our list. Now ultimately what we want to be able to do, when we run our application and we see this list of feeds here, we want to be able to get rid of them. so if we don't want to follow these feeds anymore, we want to take them off the list but not only do we want to take them off the table. We also want to delete them from core data. Now, if you've run any sort of decent apps these days, you know that if you get a, a list view like this, or a table view, and you kinda swipe over to the left here, you'll expose a button usually. Something like delete, or update, or move or something. So you'll see those types of things in like. The twitter app or you mail app or something along those lines so we would like to follow that same kind of paradigm. Now you may think that that is going to take some type of complicated UI manipulation and all that kind of stuff but honestly it doesn't. And because we are using a UI table view that is actually kind of built in for us, we just have to expose that functionality. So let's go ahead and stop this right now, so what we're gonna do, we're going to expose that delete button. It's actually extremely simple. Let's just come in here, we'll go under our did receive memory warning. And we're gonna start typing and table this and then we can see the built in functions that are available and the one that we want to add in here is this commit editing style. So, we're gonna go ahead and. Select that one. And we're not gonna put any code in here just yet. We're just going to put this particular function override, overridden into our class. We'll go ahead and save that, and we'll run this, and just by adding that particular override, I can now swipe to the left on these and expose a delete button. So isn't that nice? So I, now I have this delete button. But now I wanna be able to do something with it. So if I click it nothing is happening. So now I actually wanna delete something from this. So, a lot of this is going to be duplicate code so actually I'm going to save you the heartache and I'm just going to paste in a block of code here. Two simple methods, two simple functions. That are gonna take care of this for us. So once again, here is our overwritten commit editing style. So what we want to do is we're given this index path. This path is which one that we've selected or that we've hit the delete button on within our list. So we're going to get the particular feed model out of our feeds array based on that indexed path row, just like we've done previously. I'm going to call a delete feed function, and then we'll go over that in just a second, passing in that URL, because that's really going to be that kind of unique characteristic, or that unique piece of information that's going to identify the feeds within core data. Then once that has successfully completed we want to remove that particular feed model from our collection because we don't want it to show up anymore because remember ultimately what's gonna happen once we delete it from core data we want to take that feed out of our collection and then remove it from the table. But just removing it from this collection, this. Array is not gonna remove it form the table. To do that we're actually gonna have to call this method on our table view called reload data. Now, we'll then refresh all of the items or the rows that are found within our table view control, UI Table View, and then actually remove it from the screen. Now this delete feed is actually straight forward, very similar to what we've done in the past. I just wanted to save you. All the typing, so we're gonna get our app delegate, get our managed context, we're gonna create a fetch request on the feed entity, we're gonna create a predicate based on that URL again. Then we're going to execute that fetch request, and we're gonna see I should only get one because I should only be able to insert one record. Or one object into that core data as a feed entity that has that URL, we have that check in there, remember, when we added in that feed exists function. So if we only have one in there, then we're going to get it out. It's going to be the only one so it should be the first one. Then we're going to use the delete object method or function found in the managed object. And then ultimately, we still have to call save. So then once we call save, it's going to finish that removal of that entity from core data. And then, like we said before, it's gonna remove it from our array, and then, reload or refresh that data. So, let's go ahead and try to use this. We'll run our application. We now have these two feeds here, and I don't want to see this Xamarin Blog anymore, so we're gonna swipe to the left. I see my delete button. I'll hit delete. And, poof, it is gone. So, just to prove now that it's not there anymore, I'm gonna stop debugging. I'll bring up my iPhone simulator again. I'll open up Swift Reader, and I still only get that one feed. So, that's very nice. Now I can add feeds and I can remove feeds. But what if I want to refresh them? What's a common way that you see people refreshing data when it comes to iPhone applications? Well, we're going to have to wait and see that in the next lesson.

Back to the top