I want to set the calculated priority using the server timestamp provided by Firebase

I would like to set the priority of the child using the server timestamp provided by Firebase, ServerValue.TIMESTAMP :

 mFirebaseref.child(userid).setPriority(ServerValue.TIMESTAMP); 

But my case is the opposite. I want to set a negative ServerValue.TIMESTAMP to move my child to the beginning depending on the time. Is it possible to do this in Firebase without using the local timestamp of System.CurrentTimeInMillis() ?

I would like to do something like this:

 mFirebaseref.child(userid).setPriority(-ServerValue.TIMESTAMP); 
+5
source share
2 answers

On the client side, ServerValue.TIMESTAMP is an object structured as follows: {.sv: "timestamp"}

So, as you know, you cannot easily do what you wanted. However, there may be another solution. If, for example, you need the last five entries, you can still set priority to ServerValue.TIMESTAMP :

 mFirebaseref.child(userid).setPriority(ServerValue.TIMESTAMP); 

And then use the limitToLast() method:

 Query queryRef = mFirebaseref.limitToLast(5); 

To get the last five entries.

It can also help: Display messages in descending order.

+3
source

Basically you ask how to get a negative server timestamp, and it should work offline. I found a way, there is a hidden field that you can use. Excerpt from the 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"); } }); 
+3
source

All Articles