I would like to set the priority of a child using the server time stamp provided by Firebase, ServerValue.TIMESTAMP
:
mFirebaseref.child(userid).setPriority(ServerValue.TIMESTAMP);
But my case is inverse. I want to set negative ServerValue.TIMESTAMP
to move my child to the top based on time. Is it possible to do that in Firebase without using the local time stamp System.CurrentTimeInMillis()
?
I would like to do something like this:
mFirebaseref.child(userid).setPriority(-ServerValue.TIMESTAMP);
firestore. Timestamp. A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.
ServerValue. TIMESTAMP in data that's written to the Firebase database. It's a placeholder for the actual time on the server and will be replaced when the server receives and writes the data.
Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.
On the client side, ServerValue.TIMESTAMP
is an object structured like this: {.sv: "timestamp"}
So, as you know, you can't easily do what you wanted. However, there may be a different solution. If, for example, you wanted the five most recent entries, you could still set the priority by ServerValue.TIMESTAMP
:
mFirebaseref.child(userid).setPriority(ServerValue.TIMESTAMP);
And then use the limitToLast()
method:
Query queryRef = mFirebaseref.limitToLast(5);
To get the five most recent entries.
Also, this may help: Display posts in descending posted order
You are basically asking how to get negative server timestamp and it should work offline. I found a way, there is a hidden field you can use. A snippet from documentation:
Firebase offsetRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/serverTimeOffset");
offsetRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
double offset = snapshot.getValue(Double.class);
double estimatedServerTimeMs = System.currentTimeMillis() + offset;
}
@Override
public void onCancelled(FirebaseError error) {
System.err.println("Listener was cancelled");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With