简体   繁体   English

在Firebase Firestore中查询帖子的最有效方法

[英]Most Efficient Way To Query Firebase Firestore For Posts

I am currently creating a sort of social media app and like any social media app, it is almost crucial to display some sort of information from friends. 我目前正在创建一种社交媒体应用程序,并且像任何社交媒体应用程序一样,显示来自朋友的某种信息几乎至关重要。

I have tried various ways to display posts to users that their friends have posted but it can be slow and I doubt it is the most efficient way. 我尝试了多种方式向其朋友发布的用户显示帖子,但是速度可能很慢,我怀疑这是最有效的方法。

At first I had structured my database as following: 最初,我的数据库结构如下:

users -> (uid) -> friends(array of uid's) posts(array of post id's) other info... users->(uid)-> friends(uid的数组)posts(post id的数组)其他信息...

posts -> (post id) -> image_url other info... 帖子->(帖子ID)-> image_url其他信息...

With this setup I: 1. I took the current logged in users uid and got the document that belongs to that user under "users". 使用此设置,我:1.我获取了当前登录的用户uid,并在“用户”下获得了属于该用户的文档。

  1. I then got that users friends list as an array and looped through them. 然后,我得到用户朋友列表作为数组并遍历它们。

  2. For each uid I then queried the "users" once more and found the document that belongs to that user. 然后,对于每个uid,我再次查询“用户”,并找到属于该用户的文档。

  3. I then took the first "post id" in their "posts" array if there was one 然后,如果有一个,我会在他们的“帖子”数组中获取第一个“帖子ID”

  4. I took the post id and found the corresponding post in the "posts" collection and downloaded it. 我获取了帖子ID,并在“帖子”集合中找到了相应的帖子并下载了它。

  5. Repeat step 3 - 5 for every user id until there are no more posts, or i have downloaded 10 of them. 对每个用户ID重复步骤3-5,直到没有更多帖子,或者我已经下载了10个帖子。

That was my first approach, however there are several problems with this that I can identify and most likely several more that I havent thought of yet. 那是我的第一种方法,但是我有几个可以确定的问题,很可能还没有想到。

  1. In steps 2 and 4. In these two steps I am downloading a whole array due to the fact that firestore will not allow you to download a certain index in an array. 在第2步和第4步中,由于firestore不允许您下载数组中的某个索引,因此我将下载整个数组。 This slows down the process, image if there where 这会减慢处理过程,如果在

  2. Lets say the user scrolls down to refresh new posts then it would be difficult to load more posts because even if i start downloading where i left of, another post could be added in that time which throws the whole system of. 假设用户向下滚动以刷新新帖子,那么将很难加载更多帖子,因为即使我开始下载我离开的地方,也可能会在那个时候添加另一个帖子,从而引发整个系统的混乱。

I am also experimenting with just having a collection call posts with post documents in them attatched to each user. 我还尝试仅将集合后的帖子附加到每个用户的帖子中进行尝试。

Any other ideas? 还有其他想法吗?

If I'm understanding your problem correctly, you are trying to facilitate a joins between a user and posts (ie you want all posts that user can see ordered by time or relevance). 如果我正确地理解了您的问题,那么您正在尝试促进userposts之间的joins (即,您希望user可以按时间或相关性看到所有posts )。 I would suggest creating a Firebase Function that writes a record under a users 'feed' collection when one of their friends creates a post. 我建议创建一个Firebase函数,当其朋友之一创建帖子时,在用户的“提要”集合下写一条记录。 The record will contain a Reference to the post and the necessary info to rank/order the post. 该记录将包含对该帖子的Reference以及对该帖子进行排名/排序的必要信息。 Then you can just make two queries (the first being relatively fast and pageable) to facilitate a feed for the user. 然后,您可以进行两个查询(第一个查询相对较快且可分页),以方便用户进行供稿。 If a user acquires a new friend, then just have another Firebase Function to pull in somewhat recent (probably not from the beginning of time) posts from that friend. 如果用户获得了一个新朋友,则只需使用另一个Firebase函数即可从该朋友获取一些最新的帖子(可能不是从时间开始)。

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

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