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

4.6 Adding Feeds

All right, so now we have a nice little flow of our application working now where we're able to transition from feeds to articles to the view. We can also hit our little Plus button and show a little pop up here to enter in a URL, and we've re-factored our FeedsTableViewController just a little bit to put our logic to add a feed to the screen into its own little function here, so we can access it from other places. So for now, we're just gonna leave this AddNewFeed in here with the default RSS feed that we were, have been working with to this point. Now we want to add some code to actually be able to take the feed that we type into the text box. So that the user types into the text box, click the Add button and have it unwind the feed and ultimately populate the FeedsViewController with that new article or with that new feed. So that's a fairly simple process now. All we have to do is come into our feeds, add FeedViewController. We are going to uncomment out our prepareForSegue. And now we are gonna do the same basic thing that we have done all over the place to this point. We are gonna get access to our destination, which since we are unwinding here we are going back to our FeedsTableViewController. So we are going to get a hold of our FeedsViewController and we are going to set that equal to segue.destination. And we're going to cast that as a FeedsTableViewController, just like we've done in the past. And now what we want to do, is we want to, within that FeedsTableViewController, we want to use that method that we've just created, or that function that we just created called AddNewFeed. And we want to pass into it whatever it is the user typed into our feedUrl text field. But we can't just leave this in here because the feedUrl is actually a text field, a UI text field, and we need to use its text property to get access to the data that's there. So, we'll go ahead and hit Save. And if we were to run our application now, we would expect to be presented with a list that has our Apple Developer news feed, which is hard coded in there like we saw before. Now we can hit our Add button, and now we can type in here whatever feed we want. I happen to be a big fan of Xamarin, so we'll go ahead and we'll access the Xamarin RSS feed, which is at blog.xamarin.com/feed. And so I would suspect that once I hit my Add button, I should call my PrepareForSegue, which will in turn call the addNewFeed function on my FeedsTableViewController, and then we'll unwind and put that new feed into the table. So we'll hit Add, and we will unwind back here. Now you see we've added in our Xamarin blog entry, so I can click on News and Updates. And I'll see all the stuff in here for the Apple Developer feeds. And then I'll go ahead and un, go back and hit our Xamarin blog and I see something very interesting here. It looks to me like we have put together all the articles for both of the feeds into the one list. So let's go ahead and troubleshoot that real quick. So I have a feeling I know where that's happening. So if we come back into our code here, that is found within our FeedsTableViewController, and once we're doing our parsing, I have a feeling that when it's time to start parsing out a new channel, if we find this element, we're not reinitializing our articles array. Our article model array back to empty, which it looks here like we aren't. So let's go ahead and say that articles, we're going to reinitialize this to an empty array. So then, we'll go ahead and rebuild that. We'll hit start, and then we'll come back here. We see our Apple Developer. We'll come back into our, our add feed. We'll say that we want to go to blog.xamarin.com/feed. We'll hit our Add button. We'll transition back. So now that we see here, we have our articles here, and if we go to our Xamarin blog, now we're getting just the, just the Xamarin article feeds here, just like we were expecting to get. So that is about it as far as being able to add in some of this functionality. To be able to enter in some data, to get from the user, add that feed to our lists, and ultimately pre-populate all of these tables on the different view controllers to be able to see all the data that we'd like to see. Now one interesting problem that we're gonna start to transition into our segue into if you will, into the last section of this course is, this data doesn't live anywhere. If I were to stop my application, and I were to comment out this addNewFeed line, and we'll go ahead and save. Now if I were to re-run my application, this should come up with a blank Feeds list, which it does, and that's fine. So now if I were to come in here, I'll type in my blog.xamarin.com/feed, I'll hit Add, so it's going to parse that, add it to the list. I can look at this, that's all fine and dandy, but then what happens if I come up to my Hardware button, I hit Home or Shift+Cmd+H and we will actually do Shift+Cmd+H. We will stop debugging first. We will go back into our simulator and if I were to just open up Swift Reader again, there is nothing there. So nothing is being persisted. Now that's kind of annoying, especially in an application like this where what you ultimately want to do is continue to amass and update these feeds over time. And just be able to open up your app, read the feeds and the articles that you want to, and come back another day, or in several hours, when there's new articles to read, but not have to re-enter all the same ones over and over again. So the last section of this course is going to take you into another world of data persistence using something called core data.

Back to the top