Fetch data in firebase by ranking failing?

57 views Asked by At

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

There are 1 answers

2
ThienLD On

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