Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 645
sivaram636 Avatar asked Apr 09 '15 10:04

sivaram636


People also ask

What is timestamp Firebase?

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.

What is ServerValue timestamp?

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.

How do I get realtime data from Firebase?

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.


2 Answers

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

like image 111
Seamus Avatar answered Oct 04 '22 04:10

Seamus


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");
  }
});
like image 31
David Vávra Avatar answered Oct 04 '22 04:10

David Vávra