ios


In Firebase, how can I query the most recent 10 child nodes?


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.
I'm assuming your data actually looks like this:
someDataSet: {
longUID-1: {
timeCreated: 9999999999, // (seconds since the javascript epoch)
user_id: 1
},
longUID-2: {
timeCreated: 1111111111,
user_id: 2
},
longUID-3: {
timeCreated: 3141592653,
user_id: 3
}
}
You could automate that by calling Firebase.push({user_id: ###, timeCreated: ###}) multiple times in a for loop or any other method. Maybe you're adding news stories to a webpage, but you only want your user to see the most current stories--- IDK. But the answer to your question is to use Firebase's ref.orderByChild() and ref.limitToLast().
var ref = new Firebase("<YOUR-FIREBASE-URL>.firebaseio.com/someDataSet");
//the "/someDataSet" comes from the arbitrary name that I used up above
var sortedRef = ref.orderByChild('timeCreated');
//sort them by timeCreated, ascending
sortedRef.limitToLast(2).on("child_added", function(snapshot){
var data = snapshot.val();
console.log(data);
/* do something else with the data */
});
//The console would look like this
// Object {timeCreated: 9999999999, user_id: 1}
// Object {timeCreated: 3141592653, user_id: 3}
This happened because the program took the child with the greatest timeCreated value first and then the second greatest (value) second...
Also note, the longUID means nothing when you sort them by child and neither do the other values (user_id in this case)
Here is the documentation for:
Firebase .push() method (Sorry, I'm not allowed to post this link- I dont have enough reputation)
Firebase .orderByChild method
And also, Firebase .limitToLast method
The code: ref.queryOrderedByKey().queryLimitedToLast(10) can be used for getting the most recent 10 data. However, this is an ascending order by default.
Alternatively, you can order your data via
ref.orderByChild("id").on("child_added", function(snapshot) {
console.log(snapshot.key());
});
This also presents an ascending order by default. To change it into descending order is little bit tricky. What I would suggest it to multiply ids by -1 as shown below and then sort them.
var ref= new Firebase("your data");
ref.once("value", function(allDataSnapshot) {
allDataSnapshot.forEach(function(dataSnapshot) {
var updatedkey = -1 * dataSnapshot.key();
ref.update({ element: { id: updatedkey}});
});
});
This two SO page might be useful for you also, please check:
How to delete all but most recent X children in a Firebase node?
firebaseArray descending order?

Related Links

iPhone simulator screenshot is not in proper dimension
How to update app via Xcode 5.1.1 for iOS 9
How to load txt file to UITextView with attributedString?
How to track In-App Refund in ios app [duplicate]
Displaying custom keyboard build with storyboard on iOS
“Core Data could not fulfill a fault for…”
Swift Optional Type not Unwrapped
UIScrollView Height and width
iOS - SFSafariViewController Freezes/Hanging
MTAudioProcessingTap - produce more output samples?
Localizable file isn't working on device
Parse email verification error with Swift 2
How to change Device Identity Certificate(SCEP certificate)'s expiration date in OS X Server App(for Profile Manager)
Change background color of title (only) in UIAlertController
Can't Xcode7.1 code sign with certificate?
iOS 8 vs iOS 9 Appdelegate / ViewController method call flow

Categories

HOME
ffmpeg
office-js
xaml
afp
path
garbage-collection
google-cloud-dataproc
tsql
c++14
restsharp
prestashop-1.7
wavelet
obiee
doorkeeper
sql-server-express
cytoscape.js
command-line-arguments
nservicebus
installer
aerospike
nsmutableattributedstring
formio
swig
freetype
pwm
jet
aikau
cadvisor
wso2cep
ratchet-2
ocean
pymssql
nav
chamilo-lms
phing
mask
vtable
sinon
visualsvn-server
integer-programming
php-deployer
keyboard-maestro
clientscript
domdocument
masm32
itertools
jvm-arguments
masspay
logstash-jdbc
mit-scheme
turnjs
directsound
webtrends
xnamespace
data-fitting
submission
restbed
scrutinizer
gcloud-java
training-data
skrollr
sqlj
genetic
autopy
kindlegen
axlsx
ehcache-bigmemory
newsql
twitter-follow
words
information-schema
repaint
mui
httpservice
globus-toolkit
dirpagination
b2b
mongo-shell
apache-commons-dbcp
mosek
progressdialog
symfony-2.2
documentviewer
maven-bundle-plugin
obout
jscience
httpconnection
replay
plinqo
ryz
non-relational-database
run-length-encoding
palm-pre

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App