[ad_1]
I’ve a notes app. I usually paginate the beneath posts-userIds node by childByAutoId which works nice. I permit customers to make edits, at which level I simply replace every little thing and add an editDate to the put up itself. However that editDate is on the posts ref, not the posts-usersIds ref.
How can I paginate the posts-usersIds ref by editDate? The problem is editDates are elective, which means they may make 200 posts, however solely 2 edits and even none in any respect. Both manner if the consumer desires to see the editDates first they nonetheless have to see the postDates together with them
Order can be editDates first then postDates second or if there are not any editDates simply present them all the postDates
I used to be considering as a substitute of utilizing a 1 as the worth perhaps I ought to put a postDate or editDate (in the event that they made one) as the worth to the posts-userIds ref as in
-postId-123: 1 // this what I usually use, it has no which means, only a 1 in order that it has a price
-postId-123: exchange the 1 with the postDate and alter this to editDate after they make an edit
Perhaps I may then use ref.queryOrderedByValue() however I am unsure.
My construction is the next:
@posts
@postId-123 // childByAutoId
-uid: "userId-ABC"
-postDate: 1640898417.456389
-editDate: 1640814049.713224 // edit date is simply there if the consumer made an edit
@posts-userIds
@userId-ABC
-postId-123: 1
I paginate
let ref = Database.database().reference().baby("posts-userIds").baby(Auth.auth().currentUser!.uid)
if startKey == nil {
ref.queryOrderedByKey()
.queryLimited(toLast: 20)
.observeSingleEvent(of: .worth, with: { (snapshot) in
// get posts and set startKey
})
} else {
ref.queryOrderedByKey()
.queryEnding(atValue: startKey!)
.queryLimited(toLast: 21)
.observeSingleEvent(of: .worth, with: { (snapshot) in
})
}
[ad_2]
