gollyzoom
gollyzoom

Reputation: 549

Keeping track of posts a user has liked - flutter and firestore

Background:

I am trying to create an app where a user can scroll through posts and click "like" on some of them. I am using firestore for the backend and the whole thing is a flutter app.

Question:

I want to be able to keep track of posts that a user has liked so if the post comes up again I can have the "like" indicator already on. This will involve a call to the backend to check if the post is liked or not. However, I don't want to make a call to the backend for every post to check if the user liked it or not, since the user won't have seen/liked the vast majority of posts. Ideally, I would like to keep a local list of posts the user has seen or liked already, so I can just check the post against the local list instead of making a call to the backend. I am not sure what the best/most efficient and sustainable way to do this is.

Alternately:

I would like to be able to get an indicator for whether or not a user has liked a post in the same call that I get the post's other information. What call/database structure would be good for this?

Example:

If I like a post on tumblr and then come across it again, there is no lag between my seeing the post and the "like" indicator turning on, e.g. there is no moment when the like indicator loads from off to on, it is just already on when I see the post. This makes me thing that the post and its "like" status are being loaded simultaneously. This is the effect I am trying to achieve, and I don't really care how, but Question and Alternate were two ideas I had about it.

If anyone knows how to do this, please let me know!

Upvotes: 2

Views: 1672

Answers (2)

Waelmas
Waelmas

Reputation: 1962

Since a post can be large enough on it's own and the list of users who liked it might get too long, what I suggest is follow the structure below:

Each post is a document that contains a map with Reference to lists of users. Each of those lists is a Document by itself. This way you can have as many users as you want liking each post, and save the 1MB size limit of each Document only for the content of the post.

In my example I have all 3 documents in the same collection, but you can separate the lists of users from the posts.

Sample Post Document:

enter image description here

Sample document with list of users who liked the post:

enter image description here

Upvotes: 1

J. S.
J. S.

Reputation: 9625

The technique I've used to store user votes (or likes) on a post in a way that doesn't require multiple or complex queries on a document based database like Firestore was to save the user IDs of the users who have voted in an array in the Post document itself. Like this:

Document structure

This allows you to compare your user's ID on the app with the list IDs in the userVote array. You will immediately know if this user has voted on this post.

In the case of wanting the user's cast vote to reflect immediately on your app and not wait for the database result to reflect on your view, then you can immediately change the vote locally on the app and then have the result of the new snapshot you are listening to update the view when it arrives.

Upvotes: 1

Related Questions