在Firebase中,如何查询最近的10个子节点? [英] In Firebase, how can I query the most recent 10 child nodes?
问题描述
我使用childByAutoId来生成我的孩子。每个孩子看起来像这样:
{
user_id:1
}
我想获得最近添加的最后10个,按时间降序排序。最简单的方法是什么?
答案是您需要使用一点反向逻辑,每个节点中的时间戳记密钥值对作为负值。我省略了user_id:1以保持答案的清晰。
这里是Firebase结构
test:{
-KFUR91fso4dEKnm3RIF:{
timestamp:-1.46081635550362E12
},
-KFUR9YH5QSCTRWEzZLr:{
timestamp:-1.460816357590991E12
},
-KFURA4H60DbQ1MbrFC1:{
timestamp:-1.460816359767055E12
},
-KFURAh15i-sWD47RFka :{
timestamp:-1.460816362311195E12
},
-KFURBHuE7Z5ZvkY9mlS:{
timestamp:-1.460816364735218E12
}
}
以下是Firebase的写法。我只是用一个IBAction来写一个节点:$ b
$ b
let testRef = self.myRootRef.childByAppendingPath(test )
let keyRef = testRef.childByAutoId()
let nodeRef = keyRef.childByAppendingPath(timestamp)
let t1 = Timestamp
nodeRef.setValue(0 - t1)//记下负值
以及在
let ref = self.myRootRef.childByAppendingPath(test)
ref中读取的代码。 queryOrderedByChild(timestamp)。queryLimitedToFirst(3).observeEventType(.ChildAdded,withBlock:{snapshot in
print(The key:\(snapshot.key))// the key
} )
我声明了一个小函数来返回当前时间戳
var Timestamp:NSTimeInterval {
return NSDate()。timeIntervalSince1970 * 1000
}
和输出
:-KFURBHuE7Z5ZvkY9mlS
钥匙:-KFURAh15i-sWD47RFka
钥匙:-KFURA4H60DbQ1MbrFC1
<正如你所看到的,它们是相反的。
注意事项:
$ ol
在这个笔记上,你也可以像往常一样读取数据,并将其添加到一个数组,然后对数组进行降序排序。这会让客户端付出更多的努力,如果你有10,000个节点可能不是一个好的解决方案。
I'm using childByAutoId to generate my children. Each child looks like this:
{
user_id: 1
}
I'd like to get the last 10 most recently added, sorted by time DESC. What's the easiest way to do this?
The answer is that you need to use a bit of reverse logic, and also store a timestamp key:value pair within each node as a negative value. I omitted the user_id: 1 to keep the answer cleaner.
Here's the Firebase structure
"test" : {
"-KFUR91fso4dEKnm3RIF" : {
"timestamp" : -1.46081635550362E12
},
"-KFUR9YH5QSCTRWEzZLr" : {
"timestamp" : -1.460816357590991E12
},
"-KFURA4H60DbQ1MbrFC1" : {
"timestamp" : -1.460816359767055E12
},
"-KFURAh15i-sWD47RFka" : {
"timestamp" : -1.460816362311195E12
},
"-KFURBHuE7Z5ZvkY9mlS" : {
"timestamp" : -1.460816364735218E12
}
}
and here's how that's written out to Firebase; I just used a IBAction for a button to write out a few nodes:
let testRef = self.myRootRef.childByAppendingPath("test")
let keyRef = testRef.childByAutoId()
let nodeRef = keyRef.childByAppendingPath("timestamp")
let t1 = Timestamp
nodeRef.setValue( 0 - t1) //note the negative value
and the code to read it in
let ref = self.myRootRef.childByAppendingPath("test")
ref.queryOrderedByChild("timestamp").queryLimitedToFirst(3).observeEventType(.ChildAdded, withBlock: { snapshot in
print("The key: \(snapshot.key)") //the key
})
and I declared a little function to return the current Timestamp
var Timestamp: NSTimeInterval {
return NSDate().timeIntervalSince1970 * 1000
}
and the output
The key: -KFURBHuE7Z5ZvkY9mlS
The key: -KFURAh15i-sWD47RFka
The key: -KFURA4H60DbQ1MbrFC1
As you can see, they are in reverse order.
Things to note:
- Writing out your timestamp as negative values
- When reading in use .queryLimitedToFirst instead of last.
On that note, you can also just read the data as usual and add it to an Array then then sort the array descending. That puts more effort on the client and if you have 10,000 nodes may not be a good solution.
这篇关于在Firebase中,如何查询最近的10个子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!