Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to retrieve posts in a fashion that isn't so slow #172

Closed
Bricktheworld opened this issue Mar 25, 2020 · 12 comments
Closed

How to retrieve posts in a fashion that isn't so slow #172

Bricktheworld opened this issue Mar 25, 2020 · 12 comments

Comments

@Bricktheworld
Copy link

Hello, I am attempting to use this API to create a Reddit flutter app. I am attempting to retrieve posts using the stream.submissions(); function and doing an asynchronous for loop as such:

stream = currentUser.reddit.subreddit('android').stream.submissions(); await for (UserContent submission in stream) { Submission s = submission; _postsTemp.add(s); setState(() { _posts = _postsTemp; }); debugPrint('new posts'); } debugPrint("My name is ${currentUser.displayName}");

However, when I do this it seems that the API simply returns 100 posts all at once, which is quite slow and takes a varying number of seconds to load. Is there a way that is faster? Can I run an event as new data comes in the stream? For example, once one post has been retrieved, is it possible for me to run an event at this time while waiting for more posts to load?

Some documentation on this topic would be helpful for newbies like me :) thank you for all your work!

@Bricktheworld
Copy link
Author

Nvm, apparently calling the currentUser = await redditTemp.user.me(); before fetching posts is not a good idea if it is not necessary because it caused a lot of lag time from requesting the updated reddit client and actually fetching the posts

Don't do this if not necessary

Althought if there was still one more thing I'd like to be improved in the project, is that the documentation for things such as fetching posts and all of the non OAuth stuff is quite lacking. If this could be improved that would be great!

Thank you!

@Bricktheworld
Copy link
Author

Nvm Nvm, that was not the issue. The issue still remains that getting "best()" takes a long time, like 10 seconds slow. I'm not sure what the issue is...

@Bricktheworld Bricktheworld reopened this Mar 26, 2020
@michaelcerne
Copy link

Any updates?

@Bricktheworld
Copy link
Author

Hmmm, it seems like calling a simple .Hot() on all of the subreddits before actually listening in on the stream seems to help, but that does not fix the issue. It is still like a 3-5 second load time. Another thing i've noticed is that sometimes the load times are like 1 second and sometimes they are 10, it is very erratic and I can't seem to pinpoint the issue.

Also just in case it gets asked, I have a very descriptive user agent and changing it to something like "java" or "bot" did not increase or decrease the speeds surprisingly.

Is it possible this is just an issue with how my app is setup in reddit prefs? I'm not quite sure what else I can troubleshoot. If need be for replication, my codebase is on a github repository of mine called "apollo" if you need to take a look. Though I doubt it will help as almost all of the code is just taken from examples on here...

@bkonyi any idea what this is? or is this just on my end?

@Bricktheworld
Copy link
Author

Alright, new update @michaelcerne

The way to greatly improve this is through adding the "" in the android manifest.xml. This improves this and actually makes it possible to run the app in a compiled and installed mode lmao. I am going to keep this issue open because the delay is still like 1-2 seconds longer consistently compared to other android Reddit clients on android and I am looking to improve this speed. This might simply have to do with how fast https is with android in flutter but I'm not sure.

Thanks

@pigi96
Copy link

pigi96 commented Apr 3, 2020

@Bricktheworld, may I ask have you tried using the limit paramater?

Just wondering, because it used to take 3-5 seconds for mine too, before I noticed that default wasn't 100 but was 1000 (if I remember corretly, it actually called http a random amount of times, never looked into it though).

Setting limit to below 100 fetches data almost insantly otherwise I wouldn't know what would be actually wrong.

(edit) Actually read your above post and it seems you have set limits but still getting the delays? I'm actually trying a few stuff and everything seems to be working fine on my end.

(edit2) I looked at your code above... Considering that you said it's faster running with "flutter run --release" I'm guessing the setState() you are calling a bunch of times(for each new element added) may actually be slowing down the app.

Your repository is probably private, so I can't really help more.

@Bricktheworld
Copy link
Author

Bricktheworld commented Apr 3, 2020

@pigi96

hmm, so I have tried that before, however it seems like this time it worked! Thank you so much!

However now I have another question, how do I continue to get more posts, if I can only get like 90 at a time? Do I literally just have to put the "hot()" method in a loop to continually get more posts? Because if so, that seems like a super messy solution, but it could work!

Also I do have the set state so it only happens in multiples of 100, that way it does not slow down the app.

Thank you so much for your help again, hugely improved performance!

Also woops, will fix the private repo right now, just have to push some changes!

Edit: Okay repo has been fixed: https://github.com/Bricktheworld/Apollo-for-android
Edit 2: Okay so the important code is in "subreddit_post_view.dart" on line 37, that is where I fetch a bunch of posts for a particular subreddit

@pigi96
Copy link

pigi96 commented Apr 3, 2020

Yeah that should work as expected.

As for your other question... I do it in a way, that I only load more elements if user scrolled to/near the bottom of a page, so there is no unnecessary loads...
stream = _reddit.subreddit(subredditTitle).top( limit: 25, after: after); List<Submission> submissions = List<Submission>(); await for (final value in stream) { submissions.add(value); }

So as you are currently doing, I catch 25 of them and show them. Then if the user scrolls to the bottom of the list I just call the method again. Also the after parameter is just...
submissions.last.fullname (last element's fullname)

@Bricktheworld
Copy link
Author

Gotcha, yeah the documentation for the after really confused me, so that helped me a lot!

Thank you, I'm now going to close this issue

For the devs of draw to see: You might want to further explain how the limit works and why it loads faster and also some more docmentation on this topic would help.

Thanks everyone!

@bkonyi
Copy link
Member

bkonyi commented Apr 13, 2020

Sorry for the delay everyone. I've been pretty swamped with work and other efforts and haven't had a chance to respond here. @Bricktheworld I'll keep your feedback in mind and add more info when I can (I would be happy to accept a pull request for documentation explaining the limit parameter though :-)).

For future reference, if you ever run into issues with DRAW it might be worth checking out the documentation for PRAW as well, as this package's API is heavily inspired by PRAW and PRAW's documentation might be more descriptive in some cases (I've done my best to keep DRAW's up to par though!).

@Bricktheworld
Copy link
Author

@bkonyi All good, stay safe :)

And I am currently in the process of adding documentation and fixing the comment length bug in a pull request!

@bkonyi
Copy link
Member

bkonyi commented Apr 13, 2020

Thanks, you too! :) Excellent, looking forward to it @Bricktheworld!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants