CodeRatchet CodeRatchet - 1 month ago 6x
Swift Question

Retrieve ID of newly inserted record from Firebase table

I'm currently learning the ropes of Firebase and iOS so please bear with me. I'm currently posting to my table called

as shown here:

let postInfo = ["Description": txtPostDescription.text!, "ImageUrl": imgUrl, "Likes": 0]

var t = FIRDatabase.database().reference().child("Posts").childByAutoId().setValue(postInfo)


I'm trying to retrieve the
primary key
that gets created when I insert the new record, however I'm unable to retrieve it. I've tried
(but that was a random guess due to browsing the web and not being able to find a solid solution). Any ideas?

let postInfo = ["Description": txtPostDescription.text!, "ImageUrl": imgUrl, "Likes": 0]

var reference  = FIRDatabase.database().reference().child("Posts").childByAutoId()

let childautoID = reference.key 

Note :- Althogh childByAutoId() is a great functionality from Firebase.But prefer timestamps to store the data into when you want to create a node with a unique key.The reason why i prefer timestamps is because they can also be helpful in sorting of data...But thats just me..

Alternative :-

let timeStamp = Int(NSDate.timeIntervalSinceReferenceDate()*1000) //Will give you a unique id every second or even millisecond if you want..