【发布时间】:2021-12-31 18:31:55
【问题描述】:
我有一个笔记应用程序。我通常通过childByAutoId 对下面的 posts-userIds 节点进行分页,这工作正常。我允许用户进行编辑,此时我只需更新所有内容并将 editDate 添加到帖子本身。但是那个 editDate 在 posts 参考,而不是 posts-usersIds 参考。
如何通过editDate 对posts-usersIds ref 进行分页?问题是 editDates 是可选的,这意味着他们可能会发布 200 个帖子,但只有 2 个编辑甚至根本没有编辑。无论哪种方式,如果用户想先看到 editDates,他们仍然需要与他们一起看到 postDates
订单将首先是 editDates,然后是 postDates,或者如果没有任何 editDates,则显示所有的 postDates
我在想而不是使用 1 作为值,也许我应该将 postDate 或 editDate(如果他们做了一个)作为posts-userIds ref 的值
-postId-123: 1 // this what I normally use, it has no meaning, just a 1 so that it has a value
-postId-123: replace the 1 with the postDate and change this to editDate when they make an edit
也许我可以使用ref.queryOrderedByValue(),但我不确定。
我的结构如下:
@posts
@postId-123 // childByAutoId
-uid: "userId-ABC"
-postDate: 1640898417.456389
-editDate: 1640814049.713224 // edit date is only there if the user made an edit
@posts-userIds
@userId-ABC
-postId-123: 1
我分页
let ref = Database.database().reference().child("posts-userIds").child(Auth.auth().currentUser!.uid)
if startKey == nil {
ref.queryOrderedByKey()
.queryLimited(toLast: 20)
.observeSingleEvent(of: .value, with: { (snapshot) in
// get posts and set startKey
})
} else {
ref.queryOrderedByKey()
.queryEnding(atValue: startKey!)
.queryLimited(toLast: 21)
.observeSingleEvent(of: .value, with: { (snapshot) in
})
}
【问题讨论】:
标签: ios swift firebase-realtime-database pagination