swift - 按时间顺序快速排序 Firebase 帖子

标签 swift uitableview firebase firebase-realtime-database

我已将帖子添加到 firebase,我想知道如何根据用户发布帖子的时间按时间顺序提取帖子。

我的数据库设置如下 enter image description here

评论后的第一个节点是用户 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/

相关文章:

arrays - 快速将数组追加到数组

ios - Swift 可重用细胞图像被重用

ios - 在应用程序终止后运行代码......可能吗? (Xcode 8, swift 3)

ios - 使表格 View 中的字幕成为时间戳( Objective-C )

android - 如何使用 Android 在 Firebase 数据库中添加具有特定值的特定键?

Firebase Auth 在 Flutter 中不起作用

ios - 在场景之间传递时加载动画

ios - 当 UITextField 触发时同步向上滑动 UIView 并调整 UITableView 大小

iphone - 检测 UITableView 何时被重新排序

firebase - 在 Firebase 云函数中获取空值 onDelete()