【问题标题】:Swift - How creating (all) following users feed in Firebase?Swift - 如何在 Firebase 中创建(所有)关注用户?
【发布时间】:2016-06-10 16:56:15
【问题描述】:

我想使用 .indexOn: 时间戳(我正在使用 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
   ...

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

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

        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)
                }
    
            }
        })
    

    如果我理解这个问题,您还希望将帖子限制为特定用户。换句话说,您想要获取 user_0 创建的最新帖子。 (即和查询:哪里 xx && yy)

    有几种方法可以做到这一点

    1) 跟踪哪些帖子在用户节点内

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

    然后你可以直接获取特定的帖子。

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

    posts
       post_id_0
           owner_timestamp: user_id_0_20160611081433
    

    然后,查询帖子节点以获取从 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)
                }
    
            }
        })
    

    【讨论】:

    • 感谢您的描述,实际上我想知道如何让所有关注用户的帖子按照时间戳和限制一起排序,就像我的朋友的 Facebook 时间线一样?您能对此案发表意见吗?
    • 如果我的回答对你有帮助,请采纳。您的后续评论实际上是一个单独的问题,需要不同的答案。我建议发布它,让我们看看社区的建议。
    • 我已经想到了一个关注用户的提要。所以我在这里问过如何让所有关注用户的提要按时间戳和限制一起排序。
    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2013-11-15
    相关资源
    最近更新 更多