gpt4 book ai didi

ios - 仅下载前 n 个子节点(Firebase、Swift)

转载 作者:行者123 更新时间:2023-11-30 11:37:51 26 4
gpt4 key购买 nike

我正在尝试创建一个类似于 Twitter 的应用程序,但我的理解是,如果我将所有“tweets”作为子项添加到“post”节点,当我尝试检索这些子项时,我将必须下载所有帖子。随着应用程序的扩展,这将导致下载数百万个帖子。有没有办法只检索前 25 个帖子?

最佳答案

我最近又回到了这个问题并找到了解决方案。可以创建一个 firebase 查询,而不是实现简单的读取请求。

首先,应将一个子节点添加到“post”节点来存储帖子的反向时间戳。

let timestamp = Date().timeIntervalSince1970 //timestamp
let reversedTimestamp = -1.0 * timestamp //reverse time stamp

现在,可以通过添加 .queryOrdered(byChild: "reverseTimestamp").queryLimited(toFirst:25) 来创建查询。这将返回数据库中某个子项下的 25 个最新帖子。

ref.child("walls").child(userCredentials.getResidenceCode()).queryOrdered(byChild: "reverseTimestamp").queryLimited(toFirst: 25).observe(of: .childAdded, with: { (snapshot) in 

关于ios - 仅下载前 n 个子节点(Firebase、Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com