I am currently trying to build a social network built with Angular and Spring. I am trying to implement a pagination system on the backend.
public List<PostResponse> getAllPosts(int page){
Pageable pageable = PageRequest.of(page, 10);
return postRepository.findAll(pageable).stream().map(this::postToPostRes).collect(Collectors.toList());
}
This is working, but there is a problem. Let's say I have 15 posts on the backend currently (post1, post2, ecc...) and the first 10 of them are on an user homepage. If there's a new post and the user requests the second page, there will be a duplicate, since the last post he's viewing on the frontend side is now the first of the second page on backend side.
What would be the most efficient way to fix this?
CodePudding user response:
You can base yourself on the time of the click and select all the posts created before the time of the click, with this case you still risk having concurrency problems, but with little impact (depending on the traffic).
You can also take the last post ID seen by your user, and apply -> when the id is < to {your-post-id}