1

I am new to Firebase and I want to have my own keys while pushing new data to the database.

enter image description here

Right now I'm using the following.

uid(FirebaseAuth.getInstance().getCurrentUser().getUid())

But I want to create my own id and it must be a numeric value.

How can I create my own custom key there? Such as 0001,0002, etc.

I am asking here because I'm working on a project for online shopping. when a user adds a product to their inventory it needs to assign an id for every product. If it is not possible then just tell me no, I can accept that answer.

1 Answers1

0

You can simply perform a push() function. What that would do is create a random id for your node.

databaseRef.push(); // You have created a node with a random id.

However, if you want to create a node with a numeric id you can have a function that creates a numeric value:

public static long generateRandom(int length) {
    Random random = new Random();
    char[] digits = new char[length];
    digits[0] = (char) (random.nextInt(9) + '1');
    for (int i = 1; i < length; i++) {
        digits[i] = (char) (random.nextInt(10) + '0');
    }
    return Long.parseLong(new String(digits));
}

The function is from: 12 Digit unique random number generation in Java

You pass a length and it creates a random numeric value. The ideal length for you would be 5. Then you could go and do this:

long randomFiveDigitValue = generateRandom(5);
databaseRef.child(String.valueOf(randomFiveDigitValue)).setValue(your_object);

Another option would be to use an integer hash-code. It would decrease hash collisions, but you should be ready to handle them as with the previous function too. You could take an identifier from the order, like the date or even a mixture of factors, and do this:

//Replace all these variables with the respective value
String factors = "your_date" + "user_uid" + "product_name";
factors.hashCode(); //String date returns a integer hash value.

Here are the hashCode() collisions for 50000 randomly generated products:

COLLISIONS BETWEEN: 2 Items
ITEM 1: DATE: 24-5-2019 08:09 PM + NAME: IPHONE + UID: 4J3IF93KSKK0295[L3
ITEM 2: DATE: 24-5-2019 09:08 PM + NAME: MARKER + UID: 534KKJI903[PCO4OP5

It took a long time to generate these and I think collisions are really rare now. Like 1 to 50000 ratio. My algorithm could have been flawed, but still this way the collisions are really low.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
  • 1
    Thank you for your response.. when many user uses at the same time. if there is any chance of data collision.. example two members got the same id – Sathishkumar G Aug 07 '19 at 14:49
  • You could decrease the chances by increasing the number of digits. – Gaurav Mall Aug 07 '19 at 14:49
  • Yes, there is a chance of collision of course. You can increase the number of digits to decrease the rate of the collision, however, there is a possibility. And moreover, if the number is longer than 12 digits, then it still has the problem of human readability I guess. I think the unique hash key is better than more than 12 digits numeric number. – Reaz Murshed Aug 07 '19 at 14:52
  • Agreed. But he wants to have a numeric value for objects in a online store. – Gaurav Mall Aug 07 '19 at 14:53
  • I understand your intention. The answer is fine. However, there is a possibility that the whole thing can be messed up for choosing the bad keys for the inventory reference ID. I might have designed differently. – Reaz Murshed Aug 07 '19 at 14:57
  • Well, I'm always trying to improve my question on the suggestion of others :) A different design might be to create a secure SHA-512 hash, but this should be enough. Thanks. – Gaurav Mall Aug 07 '19 at 14:58
  • Using the method that you've given There is a possible that many users can add their products at the same time Because it's an online shopping application so that i need an numeric unique ID for every product... – Sathishkumar G Aug 07 '19 at 15:07
  • You could use a mixture of factors. Like concatenating date, user uid, product name and get the `hashCode()` for that. – Gaurav Mall Aug 07 '19 at 15:08
  • Check my latest function with the hashCode(), there is no way of collisions theoretically, but they will happen because of the hash function. However, the rate will be really low. – Gaurav Mall Aug 07 '19 at 15:11