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

5.4 Adding and Retrieving Feeds

Now that we have our nice little helper methods there let's go ahead and start to put them to use. So now if you recall, when we created our story board or when we augmented our story board to have this add feed view controller. We created an unwind segue from our add button to call into our feeds table view controller via the AddNewFeed method or function. So after it's done calling this or during the calling of this we're actually going to download and parse the data. And it's gonna use our delegate methods that are found down here that we did our did start element found char found characters as well as did an element. So now we wanna continue doing that and at the tail end of parsing out a new feed the very last thing it does is it runs through this. If it's the end of a channel if we've finished parsing a channel that I want to add that information to my feeds collection. So I can persist it or that I can show it on the UI table view. But now I also, instead of, not instead of doing that but in addition to doing that I wanna be able to save it to core data. So we're just going to call our SaveFeed method and we're gonna pass in this feed. So we'll go ahead and save that. So let's go ahead and run this application and with just that basic change we should start to interact with this functionality. So I'm gonna hit add and to save you from typing, I'm actually just going to long click here on this text box and select paste and I have my developer.apple.com in here and I can select add. Now this should start to add that in but I see a couple bad things happening. I see a little note down here that we were unable to load class named feed for the entity feed. So it's going to by default use NSManagedObject instead well that's fine it's gonna try to help me but unfortunately that isn't gonna work. So if I come into my little debug panel here, I can hit this little pause, play, continue program execution button and it's gonna bring me back out here. Now this is where I'm having a problem, I have a bad access and since I was unable to retrieve this feed from core data as a feed class. This doesn't really know what this title property is, it won't allow me to do it this way. Well the reason that this is happening is very common and you will probably run into this if you are just starting to use swift and core data. The problem that we have here is that when we asked ex code through the editor to create and ms managed objects subclass for us. What it did was it created a nice little class feed and article but within the swift programming language. It uses kind of name spacing to refer to all of your classes and types. So really although the class name here is Article if you ask swift what this class name is it's actually a SwiftReader.Article and SwiftReader.Feed. So how do we change it so that core data and ultimately the context know about this? Well, we're gonna go back into our data model and we're gonna select feed. And we're gonna come over to the right again here so we have name, we want, that's what we generated. It's called name and we also have the class name, which here is feed. So in order to get this to work we have to change this to SwiftReader.feed and the reason that it's Swift Reader is because that's the name of our project. So if we wanted to change this to something else then or if your project was something different or your structure was a little different then you would have to update it accordingly. So now that we have that set for feed, I'll change article here as well, we'll say class, we'll paste in the SwiftReader as well so now we'll save those. Now we'll come back and we'll run our application again and just to make sure that everything is getting to the same point and working again. I'm actually going to set a break point here where it failed before. So we could try to walk back through it and see how it's actually working. So lets go ahead and bring open our simulator again. We'll open this up, we will paste in our URL, we'll select add. So it's gonna download and then come back in here. So now it's back into our save feed. Here's the same place it failed before, we're not getting that error anymore. So now I can use this little arching arrow to step over. Now I'm able to do it, so that's good. So let's try to retrieve at this line we're gonna try to get an entity that's associated with article. So this is doing the same thing. Now these should work out we have a little problem here again. It can't find a class name for article, I thought I set that but maybe I didn't. So let's go ahead and stop this, we're gonna come back into, I'll delete this break point. Come back into our data model and it does seem as though that I set it to SwiftReader.Article but we'll go ahead and save that again just to make sure that it took. We'll double check to make sure that it is article copy that, we'll make sure that the types all match. Feeds table, we wanna cast that to an article. All right, we'll go ahead and try that again. In this case, we are going to set a break point here again just to be sure. We'll go ahead and add a feed, paste it in, select add. So now we're down here again we'll go ahead, there we go. I guess I just didn't save it properly and so we'll keep going, we'll keep going. And then what I wanna do is I want to skip over the monotonous stuff and we'll set a break point down here. We're actually going to save it and we'll hit the continue execution after I get rid of this break point, we'll continue our execution. So now this is the moment of truth if everything saves correctly then we should have a feed and its associated articles within core data. So I'll go ahead and hit the step over and it's stepped over the error which means it seems to have worked okay. So if I continue my execution, now we see the feeds here just like we did before. So I can select this, I can see my articles and it's all great. Hour of Code, come in here it's gonna go ahead and retrieve it. You see I'm getting some network activity and there we have it. There's our Hour of Code, I can start to look at our article. So lets come back here and now I want to try to add in another article that's the same but remember when I add in this feed, I want to check to see if it exists first. And so that's gonna be one of our last tests here with in this particular lesson. So lets go ahead and add in another one. In this case, I'm gonna do the same thing, I'm gonna paste this in again, I'm gonna select add. Ah-ha, so something's not quite right here. So we see that there's a dupe in here, we shouldn't have that. So we'll go ahead and stop this and now we're gonna come in here and we'll delete this break point. And I believe I know where the problem is in this case in that when we come in here and we start to parse this out, this did start element. Every single time we do that we're resetting this feed URL to empty or an empty string. And the problem here is that this URL is what we're using to uniquely identify them within core data. But by the time we try to save it this is null again which is not going to help anything. So let's go ahead and remove this line so that we're not actually doing that anymore and we'll save. And now before we run this, since we don't have any mechanism to currently delete the information that's in core data yet we haven't written it yet. To make sure that we're starting with a clean slate, we're actually going to delete this from the simulator. So we'll go ahead and delete this and all of its data. And then we'll go ahead and come back to our application. We'll go ahead and run it and we'll do the same thing again. We'll go ahead and add a feed, we will paste in our URL, add, now it's in here we see it, we can look at it. Lets add it again, go ahead and paste and add and it's still there so we're still have a bit of a problem. So let's go ahead and see what else we can find. And actually the interesting point here or the problem that we're seeing here is that we're always adding this feed to the feed's collection which is ultimately what's populating our UI table view. So we really, we really don't wanna follow that anymore because a lot of this logic is gonna be based on what's happening in save feed. And we wanna keep it that way and not just blindly add things to our feed's collection. So what we're gonna do here is gonna be slightly different, we're actually going to, just for now, we're just gonna comment this out. So that we're not adding it to our feeds collection that we wanna call SaveFeed. And then once we execute that SaveFeed method, if everything in here succeeds and we get past this point. And we successfully set it, and we successfully store that information into core data. Then we'll go ahead and we'll finish by adding this particular feed to our feeds collection, calling the append method on feed model. So we'll go ahead and save that, so now once we call that save, once we've saved it successfully, we'll go ahead and show it on the list. So let's try this one more time. So we'll execute our application, we will go ahead and add. We will paste this in, we'll select add. Ah-ha, now it's not showing up because actually what I did is I forgot to get rid of our app and we have information store there already so we'll go ahead and delete this. Delete, then we'll do Shift-Command-H to stop the editing there. We'll go ahead and run this one more time, so we'll go ahead and paste this URL in here. We'll add it then it gets added to our list. Now if we try to add it again, we'll select paste and we'll hit add, it will not add it. So it's gonna short circuit the application at that point but I could come in here and type in something else should I wish. So we'll type in, blog.xamarin.com/feed and we should be able to successfully put them in here so I can read about Apple news, I can read about Xamarin new and continue on my merry way. So now they have everything being saved into core data successfully when I run my application the first time I wanted to pull all the feeds that are already there. Well that's fairly simple all I have to do is come in to my ViewDidLoad function here and I'm going to just make a call in here to say I want feeds to be equal to get feeds. So now when I run my application, I would expect for it to go out to core data and then retrieve the data that I already have stored in my database or within core data. So there you have it, now we have the basics of our application where we can now retrieve any of our stored feeds. And related articles, and I can add some to it. So that's very nice but what if I want to get rid of some? What if I don't want to follow the Xamarin Blog anymore. What if I don't want to read the news and updates for Apple Developer? How do I do that? Well, that's gonna come up in the next lesson.

Back to the top