Firestore分页数据+快照侦听器 [英] Firestore Paginating data + Snapshot listener

查看:182
本文介绍了Firestore分页数据+快照侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with Firestore right now and have a little bit of a problem with pagination.
Basically, I have a collection (assume 10 items) where each item has some data and a timestamp.

现在,我正在获取前3个项目,如下所示:

Now, I am fetching the first 3 items like this:

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

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

Inside my snapshot listener, I save the last document from the snapshot, in order to use that as a starting point for my next page.

所以,有时我会要求下一页这样的项目:

So, at some time I will request the next page of items like this:

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

现在我的前端中有从索引0到索引5(共6个)的项目.干净!

Now I have the items from index 0 to index 5 (in total 6) in my frontend. Neat!

如果索引4处的文档现在将其时间戳更新为整个集合的最新时间戳,则情况开始恶化.
请记住,时间戳是根据order子句确定其位置的!

If the document at index 4 now updates its timestamp to the newest timestamp of the whole collection, things start to go down.
Remember that the timestamp determines its position on account of the order clause!

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

What I expected to happen was, that after the changes are applied, I still show 6 items (and still ordered by their timestamps)

发生的事情是,应用更改后,我只剩下5个项目,因为从第一个快照中推出的项目不会自动添加到第二个快照中.

What happened was, that after the changes are applied, I have only 5 items remaining, since the item that got pushed out of the first snapshot is not added to the second snapshot automatically.

我是否缺少有关Firestore分页的信息?

Am I missing something about Pagination with Firestore?

根据要求,我在此处发布更多代码:
这是我返回快照侦听器的功能.好了,上面我已经请求过的两种方法都用于请求第一页,然后再请求第二页

As requested, I post some more code here:
This is my function to return a snapshot listener. Well, and the two methods I use to request the first page and then the second page I posted already above

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()
    }
}

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

So again, I am asking if I can have one snapshot listener that listens for changes in more than one page I requested from Firestore

推荐答案

好吧,我联系了Firebase Google Group的同事寻求帮助,他们告诉我我的用例尚不支持.
感谢加藤理查森(Kato Richardson)解决我的问题!

Well, I contacted the guys over at Firebase Google Group for help, and they were able to tell me that my use case is not yet supported.
Thanks to Kato Richardson for attending to my problem!

对于对详细信息感兴趣的任何人,请参见此 thread

For anyone interested in the details, see this thread

这篇关于Firestore分页数据+快照侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆