Firestore Paginating数据+ Snapshot监听器

ska*_*esh 8 pagination objective-c ios swift google-cloud-firestore

我现在正在与Firestore合作,并且有一些分页问题.
基本上,我有一个集合(假设10个项目),其中每个项目都有一些数据和时间戳.

现在,我正在获取前3个这样的项目:

Firestore.firestore()
    .collection("collectionPath")
    .order(by: "timestamp", descending: true)
    .limit(to: 3)
    .addSnapshotListener(snapshotListener())
Run Code Online (Sandbox Code Playgroud)

在我的快照侦听器中,我保存了快照中的最后一个文档,以便将其用作下一页的起点.

所以,在某些时候我会要求下一页这样的项目:

Firestore.firestore()
    .collection("collectionPath")
    .order(by: "timestamp", descending: true)
    .start(afterDocument: lastDocument)
    .limit(to: 3)
    .addSnapshotListener(snapshotListener2()) // Note that this is a new snapshot listener, I don't know how I could reuse the first one
Run Code Online (Sandbox Code Playgroud)

现在我的前端有索引0到索引5(总共6个)的项目.整齐!

如果索引4处的文档现在将其时间戳更新为整个集合的最新时间戳,则事情开始下降.
请记住,时间戳决定了它在订单子句中的位置!

我期望发生的是,在应用更改后,我仍然会显示6个项目(仍按时间戳排序)

发生了什么,在应用更改后,我只剩下5个项目,因为从第一个快照中推出的项目没有自动添加到第二个快照.

我错过了与Firestore分页的内容吗?

编辑:根据要求,我在这里发布了一些代码:
这是我返回快照监听器的函数.好吧,我用两个方法来请求第一页,然后是我上面已经发布的第二页

private func snapshotListener() -> FIRQuerySnapshotBlock {
    let index = self.index
    return { querySnapshot, error in
        guard let snap = querySnapshot, error == nil else {
            log.error(error)
            return
        }

        // Save the last doc, so we can later use pagination to retrieve further chats
        if snap.count == self.limit {
            self.lastDoc = snap.documents.last
        } else {
            self.lastDoc = nil
        }

        let offset = index * self.limit

        snap.documentChanges.forEach() { diff in
            switch diff.type {
            case .added:
                log.debug("added chat at index: \(diff.newIndex), offset: \(offset)")
                self.tVHandler.dataManager.insert(item: Chat(dictionary: diff.document.data() as NSDictionary), at: IndexPath(row: Int(diff.newIndex) + offset, section: 0), in: nil)

            case .removed:
                log.debug("deleted chat at index: \(diff.oldIndex), offset: \(offset)")
                self.tVHandler.dataManager.remove(itemAt: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), in: nil)

            case .modified:
                if diff.oldIndex == diff.newIndex {
                    log.debug("updated chat at index: \(diff.oldIndex), offset: \(offset)")
                    self.tVHandler.dataManager.update(item: Chat(dictionary: diff.document.data() as NSDictionary), at: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), in: nil)
                } else {
                    log.debug("moved chat at index: \(diff.oldIndex), offset: \(offset) to index: \(diff.newIndex), offset: \(offset)")
                    self.tVHandler.dataManager.move(item: Chat(dictionary: diff.document.data() as NSDictionary), from: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), to: IndexPath(row: Int(diff.newIndex) + offset, section: 0), in: nil)
                }
            }
        }
        self.tableView?.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我问我是否可以拥有一个快照侦听器来侦听我从Firestore请求的多个页面中的更改

ska*_*esh 5

好吧,我联系了 Firebase Google Group 的人寻求帮助,他们告诉我我的用例尚不受支持。
感谢 Kato Richardson 解决我的问题!

对于任何对细节感兴趣的人,请参阅此线程