Skip to main content

Loading Older Tweets

Lesson 11 from: Building a Twitter App

Tony Hillerson

Loading Older Tweets

Lesson 11 from: Building a Twitter App

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

11. Loading Older Tweets

Next Lesson: Loading on a Thread

Lesson Info

Loading Older Tweets

The next step, luckily, is almost exactly the same. So we can get get that more cemented inter of one's head. We're gonna be loading older tweets instead this time so exactly the same in the opposite direction. So this should go fairly quickly. Um, so in this case, instead of adding ahead of you, we want to add a footer View that went clicked loads the tweets that are older than the tweet that is last in the list currently. So the way will do that. We will get the idea of the last tweet and then subtract one to sort of hack attack in Ah, the way that the way that twitter loads older tweets, it's it has a max I d parameter. So that's not saying it's not really saying tweets that air less than this I d. Exclusive. It's saying that there get the tweets that are less than this I d inclusive. So we have to subtract one, because that otherwise we would get a duplicate. Um, we wouldn't want to load the same tweet twice, so we have to subtract one there. Then we say load home timeline with the...

paging object again. Um and instead of saying that we want tweets newer than a certain idea, we say we want tweets older than a certain I d. And then we upend those tweets at the end of the list and go to the same dance where we, uh, till the adapter to Lo does in and then notified that list that has changed. So let's go look at the code. I will close up this projects and then open up Step three and go to status lis activity. So now you can see online 25 that I've added a new reference to the foot of you to a load more list item, which is the same same type of guy as the head of you. If I hit F three on this class, um, you remember that I have a way to show foot or text, so I could just reuse this view for the header or the footer. I don't have to make a different view. And then, um let's see where to start. I think, um, in load home timeline, when I call set load more views is kind of where we started last time. So online. 62 when the load when the timeline is loaded, I say Set, load more views and it's not have to I want to hit F three here that will take us down to, uh, line 102 through 109 which is where I get a reference to now a foot or view, which is inflated from the same layout as the head of you. And I tell that foot of you to show the foot or text. And then online 108 I say to the list view to add a foot of you and then passing that view. So now there's a footer and header on the list. So the next interesting things that happen, I think, are kicked off when it lists. I miss click. So online. 71 of status look, list activity. Um, now I just have extra condition in here If the view that was clicked is the foot of you that I want to show progress on the foot of you and then load older tweets instead of showing progress on the head of you and loading newer tweets. So let's go to load older tweets. I'll hit F three here online 77. Um, load older tweets against us. We're not doing this on a thread. This doesn't matter. When I had progress, I guess I put that in a weird order there. But I will get cleaned up next time. Next step. Um, and then in this case, I'm just going to get a list of status. Is that come from the home timeline? But like I said, I'm gonna pass a paging object. Um, where it sort of uses the builder pattern again, create a new paging objects and then passing a max I d of the adapters last I d minus one because that's an inclusive load. And then once I have that list of of tweets, I'll say to the adapter to upend older tweets. So let me hop into their by hitting f three here. A pending older tweets is pretty much the same as a pending newer tweets, except instead of calling insert, as I do for newer tweets, which will put objects that a specific index in the array and in this case, the index's zero. So at the beginning, we just call ad, and that puts them at the end of the array So the realist that I have so far I'll just at each newer tweet and those will get put at the end of the array or were at the later positions in the ratio. So they will be shown up, down the down the page. And again, I need to separate notifications to false and then notify that the data set has changed at the end. Okay, I think that should make sense to everybody. Let's see it in action. I think I need to You go here and say, Run is an android application and we'll see that liberty over here. So now it will pretty much work the same way when the statuses air all loaded. Um, I'm not going to see the header, but I will see as I scroll down to the bottom the footer and it will load Auteuil older tweets. And I don't have to worry about updating Twitter at all when I click low dolor tweets, it will pop in some new tweets from here on down. So this is the one that was the earliest before, and now I have a whole nother page worth here at the bottom of my list. And now I've loaded older tweets. And then the footer ends up again at the bottom as, um, awaited load. Even older tweets. He can keep on loading here until the memory runs out. Basically. Okay. Ah, is there anything else we want to look at? I think one extra thing maybe, Um, I guess I guess the only difference between loading newer tweets and older tweets is that at the end, online 86 I set the selection toe one, and I don't want to do that. When I load older to its I just want to let it sit where it was in the list. So pretty simple kind of the way to cement in the way we're doing all of this. This work, um, of loading newer tweets or older tweets. Pretty much works the same way for older. But let me let me load older tweets one more time because I want to show you something that's important. So again, just as I did when I load newer tweets up here at the top, I'm showing progress on this on this Ah, on the head of U. So that should see the the progress indicators show up, but it's not and the same way here. When I load older tweets, it just sort of sits there. Selected doesn't show the progress until the work is done. Um, and and then the tweets get appended in the the footers back at the bottom. So I'm not actually seeing that progress. And the reason for that is the summary that I want you to take away. Although everything is pretty much the same, Um, let's just focus on the fact that when that long running test, that network operation of loading those tweets this happening, um, it's running on the same thread as a U Y. Unless we explicitly put it on another thread. And there's nothing that we can see on the U I during that because the work is all taken up by the network operation. So it causes are you to become unresponsive, which is not good. So it it makes the user lose trust that our application is not just freezing up every once in a while. It's like, what's going on here? I know. I don't know what's going on. Okay, now I see that everything's loaded. I couldn't even use the you I fact here, let me go back to the to the emulator. When I click load older tweets I try to scrolling list and I cant until everything's done. See? So the us completely unresponsive. What? We're doing that network operation. So that's something I want you to keep in mind as kind of a summary for this step. Um, but, uh, are there any questions about what we went through for this step, or shall we go talk about threads? Yep. I had one question, um, one person asked When you click load, load, older tweets Is the footer getting deleted and then reinserted? Or is it just getting How's it getting moved to the bottom of the new of the new set of tweets. It just gets moved to the bottom. So that's completely taken care of by android list. Cool. All right, that was my only question. Ruth has a question. Some people are wondering how to limit the number of tweets that you get back or filter them. And what about if the list becomes too large? Is there any out of memory exception that needs to be caught? Um, so first question was, Can you filter the tweets. Yeah, with Twitter for J. There's a lot of different options you have for loading different page sizes, loading, loading, different things with paging. And I'll just point you to the A P. I docks for Twitter for J at I think Twitter for Jadot Warg. You can go look at different examples there. Um, and as for running out of memory, you can't really recover from that exception. Uh, yeah, and that's that. Exception will be thrown. At some point, we would run out of memory. Luckily, there's not too much memory that's being taken up by any tweets. Um, so that will take a while for their app to run out of memory because we're not creating, Remember, because the list view is recycling the individual list items were not actually creating your views. For every tweet that we load, we're just We just have the few K or whatever in memory for each tweet, but still, at some point you would run on a memory. But the way to deal with that is not, um, to catch an exception because there's not really waiting Teoh recover from your operating out of memory when it comes to the point where it throws an exception. There is a warning that will be passed along to either your application. Remember week we extended him to go to the code. We extended the application, um, to have a tweet application, be the application class that's constructed. And there is a method on this that we can override here. So if you got a source, override or implement methods on low memory, you can see that this this method is called on the application when the APA runs out of memory. And I think you can you can get a call back on each individual activity too, and we would want to do something like I don't know where you could either just say Hey, Hey, user, You run out on a memory because you've loaded a lot of tweets. Do you want to just kind of cut down on all the tweets that you see? And ah, I don't know. You could show just the page that they're on and get rid of tweets on either side of side of that or something where you could completely just start from new start from scratch. I don't know what the right thing to do for the user would be, but but you can take some sort of action when that one of those methods was called.

RELATED ARTICLES

RELATED ARTICLES