Swift 按时间戳对Firebase查询进行分页
假设我想检索具有以下结构的帖子,分页,按时间戳排序:Swift 按时间戳对Firebase查询进行分页,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,假设我想检索具有以下结构的帖子,分页,按时间戳排序: post_key: { message: "message" timestamp: 1234567890 } 我可以使用.queryOrdered(byChild:“timestamp”).queryLimited(toLast:5).进行初始提取。但是对于第二次提取,如果上一次提取的最后一个时间戳重复,则会出现问题 我必须以某种方式按时间戳排序,并以一个键结束。比如: .queryOrdered(byChild: "tim
post_key: {
message: "message"
timestamp: 1234567890
}
我可以使用.queryOrdered(byChild:“timestamp”).queryLimited(toLast:5).
进行初始提取。但是对于第二次提取,如果上一次提取的最后一个时间戳重复,则会出现问题
我必须以某种方式按时间戳
排序,并以一个键结束。比如:
.queryOrdered(byChild: "timestamp").queryEnding(atPOSTkey: lastPostKey)
有什么办法解决这个问题吗?我想到的一种方法是将时间戳与post键连接起来,但我认为有一种更简单的解决方案(我看到有函数queryinding(atValue,childKey)
,但似乎无法真正使其工作)
编辑1: 我想我成功地做到了以下几点:
ref.child(“posts/byLocation/\(userLocation)/\(dashedInterests)”).queryOrdered(byChild:“timestamp”).queryEnding(atValue:lastTimestamp,childKey:lastPostKey).queryLimited(toLast:postsPerPage+1).observeSingleEvent(of:.value).
问题是,现在我总是收到这样的警告:
使用未指定的索引。您的数据将被下载和过滤
在客户机上。考虑添加“.Noxon”:“时间戳”
/将/byLocation/1/1-2-3发布到您的安全规则中以获得更好的安全性
表演
我不能真正添加.indexOn规则,因为我有几个动态生成的节点,如1-2-3
(例如1、2、3、1-2、1-3等)
编辑2: 我添加了以下规则:
"posts": {
"$dashed_interests": {
".indexOn": "timestamp"
},
"byLocation": {
"1": {
"$dashed_interests": {
".indexOn": "timestamp"
}
}
}
我不再收到警告
如果有人能证实我所做的是好的,那就太好了
谢谢。我设法让它按如下方式工作:
ref.child(“posts/byLocation/\(userLocation)/\(dashedInterests)”).queryOrdered(byChild:“timestamp”).queryEnding(atValue:lastTimestamp,childKey:lastPostKey)。queryLimited(toLast:postsPerPage+1)。observeSingleEvent(of:.value)…
其中lastPostKey和lastTimestamp是上次提取的最后一个键和时间戳
还有一个问题,因为我总是收到这样的警告:
使用未指定的索引。您的数据将被下载和过滤
在客户机上。考虑添加“.Noxon”:“时间戳”
/将/byLocation/1/1-2-3发布到您的安全规则中以获得更好的安全性
表演
由于我有几个动态生成的节点,如1-2-3(例如1、2、3、1-2、1-3等),我通过添加以下规则来解决它:
"posts":{
"byLocation":{
"$location":{
"$dashed_interests":{
".indexOn":"timestamp"
}
}
}
...