跟踪用户已点赞的帖子 – Flutter 和 Firestore

huangapple go评论70阅读模式
英文:

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

问题

背景:

我正在尝试创建一个应用程序,用户可以浏览帖子并点击“喜欢”其中一些帖子。我正在使用Firestore作为后端,整个应用程序是一个Flutter应用程序。

问题:

我想要能够跟踪用户喜欢的帖子,以便如果再次显示该帖子,我可以将“喜欢”指示器已经打开。这将涉及到调用后端来检查帖子是否已被喜欢。但是,我不想为每个帖子都发出后端调用来检查用户是否喜欢它,因为用户不会看到/喜欢绝大多数帖子。理想情况下,我希望保留用户已经看过或喜欢的帖子的本地列表,这样我只需检查帖子是否在本地列表中,而不是发出后端调用。我不确定这样做的最佳/最有效和可持续的方法是什么。

或者:

我希望能够在获取帖子的其他信息的同时获取用户是否已经喜欢帖子的指示器。哪种调用/数据库结构适合这种情况?

示例:

如果我在Tumblr上喜欢一篇帖子,然后再次遇到它,我在看到帖子和“喜欢”指示器打开之间没有延迟,例如,在我看到帖子时,没有加载喜欢指示器从关闭到打开的时刻,它在我看到帖子时已经打开。这使我认为帖子及其“喜欢”状态同时加载。这是我试图实现的效果,我不太在乎如何实现,但“问题”和“替代”是我关于这个问题的两个想法。

如果有人知道如何做到这一点,请告诉我!

英文:

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!

答案1

得分: 1

我用来存储用户对帖子的投票(或喜欢)的技术,不需要在像Firestore这样的基于文档的数据库上进行多次或复杂的查询,是将已投票的用户的用户ID保存在帖子文档本身的数组中,就像这样:

这允许您将应用程序中的用户ID与userVote数组中的ID列表进行比较。您会立即知道这个用户是否已对此帖子投票。

如果您希望用户的投票立即在您的应用程序上反映出来,而不必等待数据库结果在视图上反映出来,那么您可以立即在应用程序上本地更改投票,然后在新的快照结果到达时,让您正在监听的结果更新视图。

英文:

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:

跟踪用户已点赞的帖子 – Flutter 和 Firestore

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.

答案2

得分: 1

以下是翻译好的部分:

由于帖子本身可能足够大,喜欢它的用户列表可能会变得太长,我建议按照以下结构进行操作:

每个帖子都是一个包含对用户列表的引用的文档。每个用户列表本身也是一个文档。这样,您可以让每个帖子有多少用户喜欢,同时将每个文档的“1MB大小限制”仅用于帖子的内容。

在我的示例中,我将所有3个文档放在同一个集合中,但您也可以将用户列表与帖子分开。

示例帖子文档:

跟踪用户已点赞的帖子 – Flutter 和 Firestore

包含喜欢帖子的用户列表的示例文档:

跟踪用户已点赞的帖子 – Flutter 和 Firestore

英文:

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:

跟踪用户已点赞的帖子 – Flutter 和 Firestore

Sample document with list of users who liked the post:

跟踪用户已点赞的帖子 – Flutter 和 Firestore

huangapple
  • 本文由 发表于 2020年1月6日 16:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定