2016-06-10 24 views
1

我想从使用.indexOn的下列用户获得最新的帖子:timestamp(我使用UTC时间来做到这一点)但我不知道如何过滤以下帖子作为与时间戳同时排序,以便能够使用limitToLast在这种情况下正确吗?Swift - 如何创建(所有)以下用户提供Firebase?

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 
    ... 

回答

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 & &日)

这样做有它

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)第二个选择是格式化你的火力地堡来匹配你想要得到什么:

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

     } 
    }) 
+0

感谢您的描述,实际上我不知道如何获得所有关注用户的帖子,将时间戳排序并将所有时间限制在一起,就像我朋友的Facebook时间线一样?你能否为这个案子添加你的意见? – tobeiosdev

+0

如果我的回答对你有帮助,请接受。你的跟进评论是一个单独的问题,需要不同的答案。我会建议发布它,让我们看看社区的建议。 – Jay

+0

我已经想到了一个关注用户的提要。所以我在这里问了如何让所有下面的用户通过时间戳和限制一起进行排序。 – tobeiosdev

相关问题