1

I am developing a inventory application in which a unique auton-increment value needs to be generated for creating product ids.

Little confused how to generate a custom auto-increment id using firebase as back-end service.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

3

The Firebase Database equivalent of an auto-increment columns is called a push ID. These keys are generated when you call push() (or on iOS childByAutoId) and they are:

  • guaranteed to be unique across all clients
  • always increasing
  • can even be determine while you're not connected to the server

But unlike the auto-increment columns in SQL databases:

  • Firebase push IDs are strings, not numbers
  • Firebase push IDs are not sequential

Instead of 1, 2, 3, you'll get value like "-KqB_B7Rh8cTYQxihbpb", "-KqB_CfSXYZ3kp0a_WlT", "-KqB_CuTwYXn-UtUEv0s". While they may be a bit harder to get used to, the advantages are well worth it.

I recommend reading more in these two blog posts:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Every time you insert data in Firebase, a unique id (UUID) is generated for object. The recommended practice is to use that firebase-generated UUIDs or a custom UUID, but not an auto-increment id (Check this question)

Jaime
  • 5,435
  • 2
  • 18
  • 21