Home > Mobile >  Firebase Real Time DB - DatabaseReference.push().getKey() is a TimeStamp
Firebase Real Time DB - DatabaseReference.push().getKey() is a TimeStamp

Time:11-30

According to the Documentation, the below code can set a timestamp as the key of the node using push() in the Realtime Database.

public void uploadToDB(String s) {
databaseReference.push().setValue(s);
}

The returned key are below of my push(), as an example: a) -MpfCu14jtIkEk28D3CB b) -MpfCxv_Nzv3YJ87MfZH

My question is:

  1. are they timestamp?
  2. if yes, can I decode it back to a readable timestamp?

CodePudding user response:

  1. are they timestamp?

No, those pushed IDs are not timestamps. However, it contains a time component.

As Michael Lehenbauer mentioned in this blog article:

Push IDs are string identifiers that are generated client-side. They are a combination of a timestamp and some random bits. The timestamp ensures they are ordered chronologically, and the random bits ensure that each ID is unique, even if thousands of people are creating push IDs at the same time.

And to answer the second question:

  1. if yes, can I decode it back to readable timestamp?

If you reverse the engineering, probably yes. Please check the following answer:

But would not count on that. To have an order according to a time component, then you should add a property of type "timestamp", as explained in my answer from the following post:

  • Related