按顺序订购Firebase帖子 [英] Ordering Firebase posts Chronologically Swift

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

问题描述

我已将帖子添加到firebase中,我想知道如何根据用户发布的时间顺序将帖子按时间顺序拉出.

I have added posts to firebase and I am wondering how I can pull the posts chronologically based on when the user has posted them.

我的数据库的设置如下所示

My Database is set up like below

注释后的第一个节点是用户ID,然后在其下方发布帖子.显然,这些帖子是按顺序排列的,但是,例如,如果新用户在发布"和另一个1"之间发布了一些内容,我将如何拉出它,使其显示在它们之间.

The first node after comments is the User ID and then the posts are underneath that. Obviously, these posts are in order, however if a new user posts something in between "posting" and "another 1" ,for example, how would I pull that so it shows up in between.

是否有一种方法可以删除autoID,而仅将userID用作密钥?我遇到的问题是之前的帖子被覆盖了.

Is there a way to remove the autoID and just use the userID as a key? The problem I am running into is the previous post is overwritten then.

我接受的答案是最彻底的.解决问题的方法是,将唯一键创建为第一个节点,然后将UID用作子项,并将注释用作子项.然后我按顺序拉出唯一键,并找到与uid相关的注释.

I am accepting the answer as it is the most thorough. What I did to solve my problem was just create the unique key as the first node and then use the UID as a child and the comment as a child. Then I pull the unique key's as they are in order and find the comment associated with the uid.

推荐答案

其他答案都有优点,但更完整的解决方案包括为帖子加盖时间戳和

The other answers all have merit but a more complete solution includes timestamping the post and denormalizing your data so it can be queried (assuming it would be queried at some point). In Firebase, flatter is better.

posts
   post_0
     title: "Posts And Posting"
     msg: "I think there should be more posts about posting"
     by_uid: "uid_0"
     timestamp: "20171030105500"
     inv_timestamp: "-20171030105500"
     uid_time: "uid_0_ 20171030105500"
     uid_inv_time: "uid_0_-20171030105500"

comments:
   comment_0
     for_post: "post_0"
     text: "Yeah, posts about posting are informative"
     by_uid: "uid_1"
     timestamp: "20171030105700"
     inv_timestamp: "-20171030105700"
     uid_time: "uid_1_20171030105700"
     uid_inv_time: "uid_1_-20171030105700"
   comment_1
     for_post: "post_0"
     text: "Noooo mooooore posts, please"
     by_uid: "uid_2"
     timestamp: "20171030110300"
     inv_timestamp: "-20171030110300"
     uid_time: "uid_2_20171030110300"
     uid_inv_time: "uid_2_-20171030110300"

有了这种结构,我们可以

With this structure we can

get posts and their comments and order them ascending or descending
query for all posts within the last week
all comments or posts made by a user
all comments or posts made by a user within a date range (tricky, huh)

我还另外输入了两个键:值对将其四舍五入:复合值,查询-升序和降序,时间戳.

I threw a couple of other key: value pairs in there to round it out a bit: compound values, query-ing ascending and descending, timestamp.

这篇关于按顺序订购Firebase帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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