我有多个帖子,每个帖子都有自己的评论。我想查询按时间戳排序的所有帖子。我试过了:
let query = Firebase(url: path)
let query.queryOrderedByChild("commentDate").observeEventType(.ChildAdded)
{ (queryResponse, cancelBlock) { ....
“commentDate”是服务器上的整数。在此查询中,我只获得第一个对象
let query = Firebase(url: path)
query.queryOrderedByChild("commentDate").queryLimitedToFirst(100).observeEventType(.ChildAdded) { (queryResponse, cancelBlock) { ...
我也只得到第一个对象...... 我目前的唯一解决方案是查询所有:
query.queryOrderedByChild("commentDate").observeEventType(.Value) { (queryResponse, cancelBlock) in ...
但是使用这个我必须在向用户显示之前对数组进行排序。
我有什么想法可以让它们在服务器上排序?
修改 火基数据的表示:
{
"-KGRZmSIDXz5hHkelthQ" : {
"something1" : "783151",
"something2" : "21",
"something3" : "wjeicisje ejej",
"comments" : "",
},
"-KGRa5skGzA1GAG09Lno" : {
"something1" : "783151",
"something2" : "21",
"something3" : "wjeicisje ejej",
"comments" : "",
},
"-KGVr-tti1zr1M1QLlHL" : {
"something1" : "783151",
"something2" : "21",
"something3" : "wjeicisje ejej",
"comments" : {
"-KGX1_rFSBLmQJ7QzCRc" : {
"commentBody" : "something",
"commentDate" : 1.461933727259896E9,
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
"-KGX1r5Lnhv9YbQre6as" : {
"commentBody" : "something",
"commentDate" : 1.461933797884702E9,
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
"-KGXVGKA0SYm-vRs6zsv" : {
"commentBody" : "something",
"commentDate" : 1.461941507496439E9,
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
},
}
}
答案 0 :(得分:1)
您的数据结构看起来很好,因此简单的查询将返回您想要的结果
let commentsRef = ref.childByAppendingPath("-KGVr-tti1zr1M1QLlHL/comments")
commentsRef.queryOrderedByChild("commentDate").observeEventType(.ChildAdded, withBlock: {
snapshot in
let nodeData = snapshot.value
print(nodeData!)
})
以上将返回每个子节点,按正确的顺序一次返回一个。
如果你有少量的节点,你可以一次读取它们并迭代快照。孩子这样
usersRef.queryOrderedByChild("commentDate").observeEventType(.Value, withBlock: {
snapshot in
for child in snapshot.children {
print(child)
}
})
以上两个都将打印出commentDate
命令的每个评论节点只有当您有兴趣检索每篇文章中的评论时,上述内容才有效。如果没有,您需要更改Firebase结构并将注释移动到单独的节点,如此
posts
post_01
post_02
etc
和一个单独的注释节点,它指的是注释所属的帖子。由于您要查询post_02的评论,并希望它们通过commentDate排序,请将帖子编号和日期合并为一个孩子。
comments
"-KGX1_rFSBLmQJ7QzCRc" : {
"for_post_and_commentDate": "post_02_1.461933727259896E9",
"commentBody" : "something",
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
"-KGX1r5Lnhv9YbQre6as" : {
"for_post_and_commentDate": "post_02_1.461933797884702E9",
"commentBody" : "something",
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
然后修改查询
usersRef.queryOrderedByChild("for_post_and_commentDate")
queryStartingAtValue("post_02_0").queryEndingAtValue("post_02_xxx").observeEventType(.Value, withBlock: {
你必须弄清楚你的commentDate格式,因为如果它们被存储起来更容易制作开始和结束时间戳:20160430100621作为yyyymmddhhmmss格式,那么你的时间戳可以这样查询:
starting: post_02_0
ending: post_02_99999999999999
这也使您可以灵活地查询昨天或上周发生的post_02的评论。
答案 1 :(得分:1)
如果你知道整个路径(例如dimensions/length
),Firebase可以查询嵌套的子节点。它无法处理每个子项下该路径中的动态元素(如$commentid/commentDate
)。
实现此功能的唯一方法是将必要的数据提升到可以查询的级别。例如,如果您跟踪每篇博文的lastCommentDate
:
{
"-KGRZmSIDXz5hHkelthQ" : {
"something1" : "783151",
"something2" : "21",
"something3" : "wjeicisje ejej",
"comments" : "",
},
"-KGRa5skGzA1GAG09Lno" : {
"something1" : "783151",
"something2" : "21",
"something3" : "wjeicisje ejej",
"comments" : "",
},
"-KGVr-tti1zr1M1QLlHL" : {
"something1" : "783151",
"something2" : "21",
"something3" : "wjeicisje ejej",
"firstCommentDate": 1.461933727259896E9,
"lastCommentDate": 1.461941507496439E9,
"comments" : {
"-KGX1_rFSBLmQJ7QzCRc" : {
"commentBody" : "something",
"commentDate" : 1.461933727259896E9,
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
"-KGX1r5Lnhv9YbQre6as" : {
"commentBody" : "something",
"commentDate" : 1.461933797884702E9,
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
"-KGXVGKA0SYm-vRs6zsv" : {
"commentBody" : "something",
"commentDate" : 1.461941507496439E9,
"commentOwnerImageView" : "something",
"commentOwnerName" : "something"
},
},
}
}
使用此结构,您可以按其上次评论日期查询博客帖子:
ref.orderByChild('lastCommentDate').limitToLast(10).on(...