Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: how to generate a unique numeric ID for key?

I need numeric IDs for human readability. How do I get it in Firebase?

I want numeric ID for keys, e.g. "000000001", "000000002","00000003","00000004".

The reason I need it is because these IDs will become the permanent object ID both online and offline. I want users to be able to browse that object page by just entering URL "/objects/00000001" without efforts.

I am asking here, because I want to know if this can be done without using .priority, sub-properties, etc. I guess set method can do it somehow. If it is not possible, just tell me no, I can accept that answer.

like image 311
Ivan Wang Avatar asked Mar 03 '15 00:03

Ivan Wang


People also ask

How does Firebase generate unique IDs?

If you're not familiar with Firebase's push IDs, they are the chronological, 20-character unique IDs generated when you write lists of data to Firebase from any of our client libraries.

How do I get a unique application ID?

If you want a single unique ID, you should only call UUID. randomUUID(). toString() once and store the value in something like SharedPreferences between app starts. Note that this is pretty similar to the purpose of Firebase Instance IDs, so you might also consider using that to identify the device.

Are Firebase IDs unique?

The keys generated by calling add() in Firestore are not tied to the collection on which you call add() . Instead they are random identifiers that are statistically guaranteed to be unique.


2 Answers

I'd suggest reading through the Firebase documentation. Specifically, see the Saving Data portion of the Firebase JavaScript Web Guide.

From the guide:

Getting the Unique ID Generated by push()

Calling push() will return a reference to the new data path, which you can use to get the value of its ID or set data to it. The following code will result in the same data as the above example, but now we'll have access to the unique push ID that was generated

// Generate a reference to a new location and add some data using push() var newPostRef = postsRef.push({  author: "gracehop",  title: "Announcing COBOL, a New Programming Language" });  // Get the unique ID generated by push() by accessing its key var postID = newPostRef.key; 

Source: https://firebase.google.com/docs/database/admin/save-data#section-ways-to-save

  • A push generates a new data path, with a server timestamp as its key. These keys look like -JiGh_31GA20JabpZBfa, so not numeric.
  • If you wanted to make a numeric only ID, you would make that a parameter of the object to avoid overwriting the generated key.
    • The keys (the paths of the new data) are guaranteed to be unique, so there's no point in overwriting them with a numeric key.
    • You can instead set the numeric ID as a child of the object.
    • You can then query objects by that ID child using Firebase Queries.

From the guide:

In JavaScript, the pattern of calling push() and then immediately calling set() is so common that we let you combine them by just passing the data to be set directly to push() as follows. Both of the following write operations will result in the same data being saved to Firebase:

// These two methods are equivalent: postsRef.push().set({   author: "gracehop",   title: "Announcing COBOL, a New Programming Language" }); postsRef.push({   author: "gracehop",   title: "Announcing COBOL, a New Programming Language" }); 

Source: https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push

like image 69
sbolel Avatar answered Sep 20 '22 04:09

sbolel


As explained above, you can use the Firebase default push id.

If you want something numeric you can do something based on the timestamp to avoid collisions

f.e. something based on date,hour,second,ms, and some random int at the end

01612061353136799031 

Which translates to:

016-12-06 13:53:13:679 9031 

It all depends on the precision you need (social security numbers do the same with some random characters at the end of the date). Like how many transactions will be expected during the day, hour or second. You may want to lower precision to favor ease of typing.

You can also do a transaction that increments the number id, and on success you will have a unique consecutive number for that user. These can be done on the client or server side.

(https://firebase.google.com/docs/database/android/read-and-write)

like image 28
htafoya Avatar answered Sep 22 '22 04:09

htafoya