Swift - 如何在 Firebase 中创建(所有)关注用户?

Swift - How creating (all) following users feed in Firebase?

我想使用 .indexOn: timestamp 获取关注用户的最新帖子(我正在使用 UTC 时间)但我不知道如何同时过滤按时间戳排序的关注帖子才能在这种情况下正确使用 limitToLast

posts
   post_id_0
      timestamp: _
      ownerID: user_id_0
   ...


users
   user_id_0
      following
         user_id_0: true
         user_id_1: true
      followers
         user_id_x: true
         user_id_y: true
   ...

如果您想按时间戳排序并将其限制为最后一个(即最新的)

    let postsRef = self.myRootRef.childByAppendingPath("posts")

    postsRef.queryOrderedByChild("timestamp").queryLimitedToLast(1)
       .observeSingleEventOfType(.Value, withBlock: { snapshot in
        if ( snapshot.value is NSNull ) {
            print("not found")

        } else {
            for child in snapshot.children {

                let q = child.value["ownerID"] as! String
                print(q)
            }

        }
    })

如果我理解这个问题,您还想将 post 限制为特定用户。换句话说,您想获得由 user_0 创建的最新 post(s)。 (即查询:where xx && yy)

有几种方法可以实现

1) 跟踪用户节点中的哪些 post

users
  user_id_0
     following
       xxxx
     followers
       yyyy
     3_most_recent_posts
       post_id_3: true
       post_id_2: true
       post_id_1: true

然后您可以直接获取特定的 posts。

2) 第二个选项是格式化您的 Firebase 以匹配您想要获得的内容:

posts
   post_id_0
       owner_timestamp: user_id_0_20160611081433

然后,查询 posts 节点的值从 user_id_0_ 开始并将其限制为最后一个 x

    postsRef.queryOrderedByChild("owner_timestamp")
       .queryStartingAtValue("user_id_0_")
       .queryLimitedToLast(1)
       .observeSingleEventOfType(.Value, withBlock: { snapshot in
        if ( snapshot.value is NSNull ) {
            print("not found")

        } else {
            for child in snapshot.children {

                let q = child.value["ownerID"] as! String
                print(q)
            }

        }
    })