我使用这个问题底部的分页方法,效果很好。一旦startKey
使用key
from初始化,db
下一个分页将发生,下一组帖子(子级)将附加到数据源。
我意识到,如果它key
被发布它的初始用户删除,那么一旦我尝试从中分页,key
因为它不存在,如果它存在就会被附加的孩子不会被附加,因为他们不会可访问(它们可基于该密钥访问)。
The only thing I could think of was to first check if the key exists()
and if it doesn't just start everything over from the beginning:
if !snapshot.exists() {
self?.startKey = nil
self?.datasource.removeAll()
self?.collectionView.reloadData()
self?.handlePagination()
return
}
It works fine but it's not the most fluid user experience because I'd rather just pull the posts prior to the deleted key (I have no prior reference to them).
A possibility is to just keep an array of all the previous keys and just loop through them but there's always a minute chance that those keys can get deleted by the users who posted them also.
Any ideas of how to get around this?
var startKey: String?
func handlePagination() {
if startKey == nil {
Database...Ref.child("posts")
.queryOrderedByKey()
.queryLimited(toLast: 7)
.observeSingleEvent(of: .value, with: { [weak self](snapshot) in
guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return}
for child in snapshot.children.allObjects as! [DataSnapshot] {
// append child to datasource
}
self?.startKey = children.key
})
} else {
Database...Ref.child("posts")
.queryOrderedByKey()
.queryEnding(atValue: startKey!)
.queryLimited(toLast: 8)
.observeSingleEvent(of: .value, with: { [weak self](snapshot) in
if !snapshot.exists() {
self?.startKey = nil
self?.datasource.removeAll()
self?.collectionView.reloadData()
self?.handlePagination()
return
}
guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return}
for child in snapshot.children.allObjects as! [DataSnapshot] {
// insert child in datasource at startIndex
}
self?.startKey = children.key
})
}
}
there's always a minute chance that those keys can get deleted by the users who posted them also
你是说你可以保留当前页面的键,然后循环直到找到一个未被删除的项目。在所有都被删除的情况下,您会到达键列表的末尾,并且应该创建一个没有 startAt() 的新查询。这将为您提供第一个 X 项,我认为在这种情况下这是正确的行为。
但总的来说:处理实时和分页真的很难,这是 FirebaseUI 中的分页适配器不进行实时更新的主要原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句