gpt4 book ai didi

Swift Firebase 按最后 10 个排序,然后显示 10 个新值

转载 作者:行者123 更新时间:2023-11-30 13:05:32 25 4
gpt4 key购买 nike

我目前在通过 tableview 显示值时遇到问题,因为我正在创建这个类似 Instagram/Facebook 的 feed,其中 tableView 在底部更新(只需通过 firebase 从数据库中获取新值)。然而,我真的很难用非重复的数据填充 tableView。我目前正在按子项(时间戳)对我的 firebase 引用进行排序,默认情况下我显示“queryLimitedToLast(10)”。然而,问题来了。我真的找不到显示"new"数据的方法,这些数据尚未在上面的表格 View 中列出。

我的 ViewController 中有一个“curLength:Int = 10”(显然是变量)。

每次用户更新tableView时,curLength都是+= 10,后面跟着一个简单更新tableView的函数。不幸的是,我无法真正弄清楚如何显示查询中不存在的数据,因为我通过“queryLimitedToLast(x)”运行它,在这种情况下,它只是用相同的数据重新填充 tableView 两次,然后10 条新记录。

如果我想显示最新数据,每次更新“10 个新帖子”,我该如何处理?

最佳答案

您需要使用时间戳作为分页光标。如果您请求按时间戳排序的 10 个帖子,请从 tableView 中具有最新时间戳的帖子的值开始查询接下来的 10 个帖子。这样,您将只获取比您最近的帖子更新的帖子。

// will give you posts before most recent timestamp
let beforeRef = FIRDatabase.database().referenceWithPath("posts")
.queryOrderedByChild("timestamp")
.queryEndingAtValue(mostRecentTimestamp - 1)
.queryLimitedToLast(10)

// will give you posts after most recent timestamp
let afterRef = FIRDatabase.database().referenceWithPath("posts")
.queryOrderedByChild("timestamp")
.queryStartingAtValue(mostRecentTimestamp + 1)
.queryLimitedToLast(10)

beforeRef.observeEventType(.ChildAdded, withBlock: { snap in
print(snap) // prepend the snap to your datasource
})

afterRef.observeEventType(.ChildAdded, withBlock: { snap in
print(snap) // append the snap to your datasource
})

关于Swift Firebase 按最后 10 个排序,然后显示 10 个新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421105/

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