ios - 在 Firestore 中使用降序时间戳进行分页

标签 ios swift firebase google-cloud-firestore

我在 Firestore 中按降序时间戳对数据进行分页时遇到问题(首先获取最新的数据)。当我订购时

Firestore.firestore().collection("items")
        .order(by: "date", descending: true)
        .limit(to: 3)

它可以工作,但如果我尝试刷新它只会加载相同的数据。为了分页我尝试过

Firestore.firestore().collection("items")
        .order(by: "random", descending: true)
        .start(after: [lastFetchedItem?.random ?? ""])
        .limit(to: 1)

它根据随机数生成器获取数据,并且每次都完美地分页,所以我知道它是可能的。现在如果我尝试

Firestore.firestore().collection("items")
        .order(by: "date", descending: true)
        .start(after: [lastFetchedItem?.date ?? ""])
        .limit(to: 1)

我加载了第一个项目,但无法分页到下一个项目。我用 Timestamp(date: Date()) 记录时间戳,在我的字典中我的日期写为

var date: Date?
    
    init(dictionary: [String: Any]) {
        self.date = dictionary["date"] as? Date
    }

最佳答案

要分页,请传入上一页最后一项的完整快照。像您这样传递单个值是行不通的。

因此,如果您的 lastFetchedItem 是文档快照:

Firestore.firestore().collection("items")
        .order(by: "date", descending: true)
        .start(after: lastFetchedItem)
        .limit(to: 1)

关于ios - 在 Firestore 中使用降序时间戳进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63679263/

相关文章:

Android - FirebaseApp/Firebase 初始化未启动

ios - AppDelegate Touch3D Xcode - TabBar

Swift 4 使用 Alamofire 和 Codable 编写发布请求

iphone - IOS。 viewControllers 数组的错误?

ios - Swift 数组 - 使用 AnyObject 类型的 "Contains"

xcode - Split View Controller : How to connect Master View Controller to Detail View Controller?

javascript - Angular 4 Firebase 操作 Observable 的 *ngFor 或在 Observable 中使用索引

android - 来自 Firebase storageReference 的 getBytes 不起作用

ios - 无法启动/private/var/containers/Bundle/Application/.app

ios - 仅当模型对象包含图像 url 时如何显示 ImageView