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

5.6 Refreshing Feeds

Now we can insert, retrieve, and delete feed entities and their associated articles from core data. The last thing within our application that we're really concerned with is being able to update. Now, I went back and forth on different ways to be able to update this and at what points you can update this. So I just kinda made a choice. You can definitely pick different routes to go about updating. Or, in different view controllers or really wherever you want to. I'm going to teach you the basic fundamentals of how I chose to do it and then you can obviously change it as you see fit. So one thing that I like in many applications that are out in the wild these days, especially when it comes to ones that use the UI table view controller. Whenever I am working on something like this in a list of data, I like to be able to click and drag down and get that little refreshing circle at the top that's going to show me that's it's doing some refreshing or some updating. And as soon as it's done, it goes away and I can just go about my merry business. So I would like to duplicate that somehow. So you can either chose to do it on the feeds view controller, or you can do it on the articles view controller it's really actually up to you. I chose to do it on the feeds so that I could drag this down and then update all of my feeds, and then be able to see any new articles that have shown up since the last time I added this. So obviously you're gonna want to refresh not only the table that's here but the data that's behind it as well as these entries for feed and articles in core data. So let's go ahead and see how I chose to do this. So go ahead and stop debugging. This is actually quite simple once again to be able to add that kind of pull down to refresh functionality. And so all I'm gonna do here is I'm gonna come into my feeds table view controller. Into my viewDidLoad function. And then I'm going to add in a little bit of work here. So I'm going to create what's known as a refresh control. So this is actually added into iOS, so this actually makes doing this type of work quite easy as a matter of fact. So I'm going to create this as a new UIRefreshControl. And then I'm going to go into my RefreshControl and I'm going to add a target. And, now a target is really just a way to associate this RefreshControl with this controller. And, then when I use it to perform some sort of action via selector, which is really just a way to kind of point to a function somewhere, that it should execute when this is actually going on. So I want the target to be myself, because this is ultimately where, within the feeds table view controller that I'm actually going to create this action, or this selector. So I'm going to reference a selector here. And I'm just going to call this Refresh feeds, like this. And then for control events, this is going to be for value change, so this is just going to be for value trained, changed control events. So that is just about all you have to do to create the refresh control. Then there's actually a property on our view controller here, that actually allows us to set a refresh control. And we're gonna set that to our refresh control and that's pretty much it. The other thing left that you have to do is actually create this function here just so that it doesn't blow up in your face. So we're just gonna create a new function called refresh feeds and we're gonna leave it at that. So we're gonna go ahead and save that. And I'm gonna show just by adding these thr, three lines of code, and a simple function here that's not actually doing anything yet, is going to give us that functionality. So we'll go ahead and run our app again. Here is our feeds view controller. Now, if I were to click and drag down, you're gonna get that little refreshing, I'm doing something control. Now unfortunately, at this point, it's never gonna stop, and the reason it's never gonna stop is because we haven't told it to stop. So we have to add that little piece in as well, and it's quite simple. All you have to do is reference your refresh control and say that you have ended refreshing. So we'll save that. So now, when we run our application, we can go ahead and drag down to refresh and then it's gonna go away immediately. So that is perfect now we just have to slide our logic in there to do our updating. So I'm going to override this function here or overwrite it with some custom code that I've already built. And once again, you can take this or leave it. If you want to leave this here, feel free. If you want to move it somewhere else, that's fine too. I'm not saying that this is best practice. This is just definitely one way you can go about doing this process. So what I've done here is I've created my refresh feeds function. I'm going to loop through all of the feeds stored within my feeds array, and I'm going to delete them from core data. So those feed entities and their associated articles will be gone at this point. Then, I'm going to make a copy of the feeds array. And then I'm going to get rid of those feeds that are found within that array so that I can re-add them in the next step. And then I'm going to go loop through all of the old feeds and I'm going to re-add them as new feeds. And if you call, if I, if you recall, if I call my AddNewFeed, it's gonna go through each one of these individually. And then download and parse them and then part of that process is to save them into the database and then reload. And then add them to the feeds array so that we can see them again. And then once we're all done we're just going to end refreshing here and then probably just to be on the safe side we'll come in here to our table view and we'll reload the data. Just to be on the safe side. So now we'll go ahead and run our application. And I will set some break points in here just so you can kind of see what's going on. And I will pull down to refresh, and there we have it. We've hit our break point, so now we can step through here, we're deleting feeds. It ran through that delete feeds twice, which is what we would expect. Now we're going to get rid of all of the feeds. Or reinitialize feeds to be empty. Then we're going to loop through all of the old feeds. And we're going to add them individually, one at a time. There we go. Then we'll reload our data. We'll end refreshing and then we will go ahead and come back. So now everything has been refreshed now not only within UI but also within core data and you can go ahead and look in here again. Now, obviously you'll have to wait for these news feeds to update and refresh to actually get the new data in here. But, using this process you will be able to see any new articles as they arrive. So now you have a fully functional application that's going to allow you to add feeds, retrieve feeds, delete them, and refresh them whenever you want. And that's a handy little utility to use even if its not going to make millions on the app store it could still be beneficial to you.

Back to the top