我在 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/