0

I am attempting to create a trending feature in my app.

Current approach:

I have a ranking child for each post in the explore timeline which is constantly updated on users phones when they view, or like.

ExploreTimeline
    post1
       ranking: 117117117
       timestamp: 32930.489349

On the explore I would then fetch the 10 highest ranked posts, when the user reaches a certain point after scrolling I paginate and fetch the next 10 highest Ending at the last ranking value.

PROBLEM

If while a user is on the explore, the ranking for the last postRanking changes, well then the next pagination will be messed up (In my testing it duplicates many posts).

How can I change my strategy to avoid this problem? Or what is a fix?

1 Answers1

0

If you want live update on ranking, then you cant implement pagination, atleast with Firebase since the order will get messed up if an update happen. Check out this video explaining pagination from Firebase themself to see why. Your best bet is to get the first 10, then if the user keep scrolling, get the first 20, 30, and so on. This way you can ensure the ranking your user get is always the correct one

ThienLD
  • 741
  • 4
  • 14
  • But this seems highly inefficient, at some point I may be fetching up to 100 items, is there really no other way? –  Aug 17 '20 at 23:11
  • well, if you want your ranking to be always correct, then it's the only way. The video i linked talked about this issue. Chances are your user won't scroll down too far for this to be problematic, since 30-40 documents per request seems reasonable – ThienLD Aug 18 '20 at 06:04