我已将帖子添加到 firebase,我想知道如何根据用户发布帖子的时间按时间顺序提取帖子。
评论后的第一个节点是用户 ID,然后帖子就在其下面。显然,这些帖子是按顺序排列的,但是,例如,如果新用户在“posting”和“another 1”之间发布某些内容,我将如何拉动它,使其显示在两者之间。
有没有办法删除 autoID 并仅使用 userID 作为 key ?我遇到的问题是之前的帖子被覆盖了。
我接受答案,因为它是最彻底的。我为解决问题所做的只是创建唯一键作为第一个节点,然后使用 UID 作为子节点,使用注释作为子节点。然后,我按顺序提取唯一键并找到与 uid 关联的注释。
最佳答案
其他答案都有优点,但更完整的解决方案包括为帖子添加时间戳和 denormalizing your data所以它可以被查询(假设它会在某个时候被查询)。在 Firebase 中,越扁平越好。
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"
通过这个结构我们可以
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)
我在其中添加了一些其他键:值对以使其更加圆整:复合值、查询升序和降序、时间戳。
关于swift - 按时间顺序快速排序 Firebase 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47005529/