2016-11-29 71 views
0

我得到了Instagram的应用程序。 我火力点作为后端其中有:Firebase复杂查询 - iOS Swift

后节点 - 包含所有帖子来自不同用户 [POST_ID post_user POST_NAME POST_CONTENT]

问题: 我只想从得到的帖子我跟随的用户。

我的步骤:

  1. 我在阵列下面users_id列表

    followersIds 
    
  2. 我想知道如果我可以让查询像

    getFollowersList { (followersIds) in 
    
        print(followersIds) 
    
        self.postRef.queryOrdered(byChild: "user").queryEqual(toValue: followersIds).queryLimited(toLast: limit).observeSingleEvent(of: .value, with: { (snapshot) in 
         if snapshot.value is NSNull { completion(nil); return} 
         else { 
          let dict = snapshot.value as! [String:Any] 
          self.mapDictionaryToArrayOfPost(dict: dict, completion: { (posts) in 
           completion(posts) 
          }) 
         } 
        }) { (error) in 
         debugPrint(error.localizedDescription) 
        } 
    
    } 
    

问题: 火力地堡不能接受queryEqual数组作为参数()

我可以在应用程序过滤做这个过滤器,但我想查询火力并得到结果。 因为在应用程序中过滤不是一件好事。

任何建议。

回答

2

无法在Firebase中一次执行此查询。在大多数NoSQL解决方案中很常见,您应该以允许应用程序需要的用例的方式对数据进行建模。

这意味着:如果您想要一次性检索您关注的人的所有帖子,则应该列出您在数据库中关注的人的所有帖子。既然你似乎是建立就像一个社交网络,这意味着你基本上建立每个用户的墙:

following 
    uid1 
    uid2: true // so uid1 follows uid2 and uid3 
    uid3: true 
    uid3 
    uid2: true // and uid3 follows uid2 
posts 
    post1 
     author: uid2 // uid2 has made a post 
     title: "...." 
    post2 
     author: uid3 // uid3 has made a post 
     title: "...." 
wall 
    uid1 
    post1: true 
    post2: true 
    uid3 
    post1: true 

因此,在这个模型中,我们保持一个用户的每一个职位,你跟着/wall/myuid下的关键。现在,您可以轻松获取帖子列表以显示特定用户的墙。从中你可以遍历按键并加载它们中的每一个。后者对Firebase来说不是一个缓慢的操作,因为它处理了请求。见Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

这种类型的数据复制是NoSQL数据库,如火力地堡,这就是为什么我们在documentation on structuring data覆盖它,在我们的firefeed demo app和我们的新的视频系列Firebase for SQL developers非常普遍。