简体   繁体   English

Firebase 中最高效的投票系统

[英]Most efficient voting system in Firebase

I'm working on an app that lets users upload pictures and rate (so vote with stars 1-5) each others pictures.我正在开发一个应用程序,它允许用户上传图片并为彼此的图片评分(所以用 1-5 颗星投票)。 But I keep thinking that the database system that I'm building is not efficient enough.但是我一直认为我正在构建的数据库系统不够高效。 I hope that there is someone who can give me advice.我希望有人可以给我建议。

Right now I have a database table called "images".现在我有一个名为“图像”的数据库表。 It has the following fields:它有以下字段:

  • doc_id文档 ID
  • user_id用户身份
  • image图像
  • amount_of_votes票数
  • rating评分

Everytime a user votes the amount_of_votes gets a +1 and the rating gets calculated by (rating*old amount of votes + new vote) / new amount of votes每次用户投票时,amount_of_votes 都会获得 +1,并且评分的计算公式为(评分*旧票数 + 新票数)/新票数

But this gives me the following problems:但这给了我以下问题:

  • If 2 users look at the same image at the same time (for example an iamge with 2 votes with 1 star) then if the first user votes 5 stars a new rating is calculated.如果 2 个用户同时查看同一张图片(例如,一张图片有 2 票,1 星),那么如果第一个用户投票 5 星,则会计算新的评分。 But if the second user votes 1 star again then the last user who votes still gets 2 votes with 1 star and doesn't see the new 5 star rating.但是如果第二个用户再次投票给 1 星,那么最后一个投票的用户仍然获得 2 票,1 星,并且看不到新的 5 星评级。
  • Also I think that it's not safe to do it this way.我也认为这样做是不安全的。 A user can in theorie just change the amount to 1 and the rating to 1.理论上,用户可以将数量更改为 1,将评级更改为 1。

I want to build a new voting system where there is a new table called votes and it holds the user_id and the vote that the user gives.我想构建一个新的投票系统,其中有一个名为 votes 的新表,它保存 user_id 和用户提供的投票。 But that gives me the following problem:但这给了我以下问题:

  • if a user clicks on a really popular image then if has to load a lot of votes and calculate an average.如果用户点击了一个非常受欢迎的图片,则必须加载大量投票并计算平均值。 That isn't efficient.那效率不高。

Does anyone know what the best system is that I can use?有谁知道我可以使用的最佳系统是什么? I have spend days on the internet but I haven't found the best system yet...我在互联网上花了几天时间,但我还没有找到最好的系统......

I didn't try implementing it myself but my approach for this will be the use of a Snapshot listener .我没有尝试自己实现它,但我的方法是使用Snapshot listener

It can help you to have updated information in the application whenever something changes in the document.它可以帮助您在文档中发生变化时更新应用程序中的信息。 The voting system which I use is collecting 5 stars, 4 stars , 3 stars, etc, separately.我使用的投票系统是分别收集 5 颗星、4 颗星、3 颗星等。 For example: how many 5 stars ratings a user got, how many 4 stars a user got and so on.例如:用户获得了多少 5 星评级,用户获得了多少 4 星等。 Then, to calculate the average rating I use the following formula:然后,为了计算平均评分,我使用以下公式:

((no_of_votes_for_5stars * 5) + (no_of_votes_for_4stars * 4) + (no_of_votes_for_3stars * 3) + (no_of_votes_for_2stars * 2) + (no_of_votes_for_1star * 1)) / (no_of_votes_for_5stars +no_of_votes_for_4stars + no_of_votes_for_3stars + no_of_votes_for_2stars + no_of_votes_for_1star )

You can save the number of stars data separately in the Firestore database and whenever someone rates a photo, you can increment the current count.您可以在 Firestore 数据库中单独保存星级数据,并且每当有人对照片进行评分时,您都可以增加当前计数。 And you can implement the above formula inside the app code so your application gets the number of stars info and calculates the average using the above formula, and then displays it.您可以在应用程序代码中实现上述公式,以便您的应用程序获取星数信息并使用上述公式计算平均值,然后将其显示出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM