我有一個筆記應用程式。我通常對下面的post-userIds節點進行分頁,通過childByAutoId它可以正常作業。我允許用戶進行編輯,此時我只更新所有內容并向帖子本身添加一個 editDate。但是那個editDate 是在posts ref 上,而不是posts-usersIds ref。
如何通過editDate對posts-usersIds ref進行分頁?問題是 editDates 是可選的,這意味著它們可能會發布 200 個帖子,但只有 2 次編輯,甚至根本沒有。無論哪種方式,如果用戶想先查看 editDates,他們仍然需要查看 postDates
順序首先是editDates,然后是postDates,或者如果沒有任何editDates,則只顯示所有postDates
我在想,而不是使用 1 作為值,也許我應該將 postDate 或 editDate(如果他們做了一個)作為posts-userIdsref的值,如
-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
})
}
uj5u.com熱心網友回復:
Firebase 查詢只能對它考慮回傳的節點下(在固定路徑中)的值進行排序/過濾。所以你不能posts-userIds從 under 取值posts。
解決方案是復制您想要在 under 上排序/過濾posts-userIds的值,直接作為值(在這種情況下您將queryOrderedByValue用于訂單)或在子屬性中(在這種情況下您將queryOrdered(byChild:)用于order. 我傾向于支持后者,因為一旦有一個你想要排序/過濾的值,通常會有更多的值。
這種資料復制在 NoSQL 資料庫中很常見,所以如果您還不習慣它,我建議您閱讀NoSQL 資料建模,并觀看Firebase for SQL 開發人員。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/399864.html
