0

I wanted to save data with my custom push id in firebase database.I don't want to use firebase generated push keys as it is difficult me to do retrieved it back when needed.Someone have any idea how can I save my data with a custom auto-generated key to firebase database.I have googled enough but I didn't find any solution suitable enter image description here

as u can see in above image I wanted to save my new record with id 44,45,46 and so on but when I push data firebase generate id which i don't want.Or else if anybody knows how do I retrieve this firebase generated id if I wanted to update or delete a particular record from the database. Any help will be appreciated

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
Mahi Parmar
  • 515
  • 3
  • 10
  • 31

1 Answers1

2

To set a custom id you could do something like this:

FirebaseNodeName.child("user").child(customId).set(key, value);
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
  • how could be it auto generated ?? if i wanted to save more then one record then same record will be updated rather then inserting new record – Mahi Parmar Jan 29 '18 at 05:53
  • you have to generate it in the backend with some logic. Eg: fetch the last pushed id and increment it by one or keep a local counter. Basically, it varies according to your use case. – BlackBeard Jan 29 '18 at 05:55
  • Moreover, to avoid "duplicate sequential keys" firebase provides it's own solution (ie unique key/ids). – BlackBeard Jan 29 '18 at 05:57
  • what is that ? can please elaborate it for me please i am new to firebase. are you talking about push() ? but it generate keys like "-L-BOesPXukRflreyooJ" and i dont know how to retrived this kind of keys if i wanted to update or delete some recods – Mahi Parmar Jan 29 '18 at 05:59
  • The random string you are seeing as the firebase autogenerated key is known as `FIrebase push IDs`. It guarantees uniqueness of each key no matter the concurrency. As a result, even if multiple users pushes data to the database concurrently, no data gets overwritten due to this unique key. It is pretty hard to achieve the same effect on the backend if there is multiple clients present(who are trying to update the table at the same time). – BlackBeard Jan 29 '18 at 06:06
  • then how do i get that `firebase push IDs` if i ever wanted? – Mahi Parmar Jan 29 '18 at 06:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164064/discussion-between-niyanta-and-blackbeard). – Mahi Parmar Jan 29 '18 at 06:16
  • I'd suggest you to please ask another question with the exact scenario. IMHO this was a question about setting `custom push ids` while pushing data to the DB. Also before asking please see [this](https://stackoverflow.com/questions/16637035/in-firebase-when-using-push-how-do-i-pull-the-unique-id) and [this](https://stackoverflow.com/questions/44289557/retrieving-data-from-firebase-with-certain-id), you might find them useful. – BlackBeard Jan 29 '18 at 06:25