Firebase Firestore,查询用户朋友的帖子 [英] Firebase Firestore, query a users friend's posts

查看:50
本文介绍了Firebase Firestore,查询用户朋友的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用Firebase创建社交媒体供稿.我的数据结构如下:

I am looking create a social-media feed using Firebase. My data is structured like this:

users: {
 uid: {
   ... // details  
  }
}

friends: { 
  uid: {
    friends: { // sub collection
      fuid: {
         ... // details
     }       
    }
  }
}`

posts: { 
  postId: {
    postedBy: uid
    ... // details
  }
}

现在,我正在尝试从用户的所有朋友那里获取帖子,将其限制为最近的10个帖子,然后创建一个滚动指令来查询下一组10个帖子,从而使用户不必查询并加载好友的帖子^ N,页面加载^ N.但是我不确定如何以这种有效方式查询用户的朋友,然后是他们的帖子的firebase.

Now I am trying to get the posts from all friends of the user, limit it to the most recent 10 posts, and then create a scrolling directive that queries the next set of 10 posts so that the user doesn't have to query and load posts^N for friends^N on the page load. But I'm not really sure how to query firebase in an effective manner like this, for the user's friends and then their posts.

我的滚动指令有效,取自 Jeff Delaney关于AngularFirebase的无限滚动课.com.,但是它只能整体处理帖子集(教程中的船只),而不会选择性地在该集合中进行查询(以检查用户是否是朋友).

I have the scrolling directive working, taken from Jeff Delaney's Infinite Scrolling Lesson on AngularFirebase.com. But it only handles the posts (boats in the tutorial) collection as a whole, without selectively querying within that collection (to check if the user is a friend).

我能想到的唯一解决方案是查询用户的所有好友帖子,将其存储在数组中,然后根据最后一批帖子将结果分块加载到DOM中.如果用户有100个朋友,每个朋友有100个帖子,那么从长远来看,这似乎真的很低效.

The only solution that I could think of was to query all of the user's friends posts, store that in an array, and then chunk load the results in the DOM based on the last batch of posts that were loaded. This just seems like it could be really inefficient in the long-haul if the user has 100's of friends, with 100's of posts each.

推荐答案

我最终通过利用Firebase Functions解决了这个问题.我有两个集合,一个叫做 Posts ,另一个叫做 Feeds .当用户添加帖子时,它将被添加到 Posts 集合中.发生这种情况时,它将触发Firebase功能,然后该功能将获取发布用户的UID.

I ended up solving this issue by leveraging Firebase Functions. I have two collections, one is called Posts and the other is called Feeds. When a user adds a post, it gets added to the Posts collection. When this happens, it triggers a Firebase Function, which then grabs the posting user's UID.

一旦有了UID,它将查询另一个名为 Friends/UID/Friends 的集合,并获取其朋友的所有UID.

Once it has the UID, it queries another collection called Friends/UID/Friends and grabs all of their friend's UID's.

一旦具有UID,就会创建一个批量添加(如果用户有500个以上的朋友),然后将帖子添加到他们朋友的 Feeds/UID/Posts 集合中.

Once it has the UID's, it creates a batch add (in case the user has more than 500 friends), and then adds the post to their friend's Feeds/UID/Posts collection.

我选择这条路线的原因有很多.

The reason I chose this route, was a number of reasons.

  1. Firebase不允许您使用数组列表(用户的朋友)进行查询.
  2. 我不想过滤掉来自非朋友的帖子.
  3. 我不想将过多的数据下载到用户的设备.
  4. 我必须按从最新到最旧的顺序对结果进行分页.

通过使用上述解决方案,我现在能够查询 Feeds/UID/Posts/集合,从而每次都返回下10个结果,而不会出现性能或数据问题.我无法完全解决的唯一限制是,将帖子添加到用户的个人供稿中需要花费几秒钟的时间,因为该功能需要一些时间来启动.但这可以通过增加该特定功能的内存分配来缓解.

By using the above solution, I am now able to query the Feeds/UID/Posts/ collection, in a way that returns the next 10 results every time, without performance or data issues. The only limitation I have not been able to get around completely is it takes a few seconds to add the post to the user's personally feed, as the Function needs time to spin up. But this can be mitigated by increasing the memory allocation for that particular function.

我也对已编辑和/或删除的帖子进行了上述操作.

I also do the above listed for posts that are edited and or deleted.

这篇关于Firebase Firestore,查询用户朋友的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆