Relative Content

Tag Archive for databasepagination

How can I implement cursor pagination when sorting by dynamic attributes such as the number of likes on comments?

I want to use cursor pagination to fetch comments from a database based on likes from most likes to least likes and load them in a comments section. This functionality is meant to mirror youtube’s ‘Top comments’. An example is that I load a set of comments and use the last comment as a cursor. Let’s say the last comment has 10 likes. The next set of comments to be loaded should have likes fewer or equal to 10 likes. A problem that occurs is that before I load the next set of comments, during this time comments that have fewer likes may have gained more likes (e.g., from 9 to 12) than the cursor and comments that I have already fetched and loaded may lose likes and make it have fewer likes (e.g., from 14 to 7) than the cursor. So when I load the next set of comments, those loaded comments may exclude comments that have recently gained likes and/or load the same comments that have already been loaded.