Swift Firebase Get childByAutoId Key When Setting

2636 views ios

I would like to obtain the .childByAutoId() key value while I am setting a firebase value. Is this possible to do or do I have to recall firebase again to retrieve it?

ref.child("users").child(user.id).child("alerts").childByAutoId().setValue(["autoID" : .childByAutoIdValue])

For Example: I need the AutoId that it generates but would like to obtain it while setting a firebase value.

let keyValue = ref.childByAutoId.key //Completley wrong but to better understand what im trying to do

*Update: This is how i achieved what I was trying to do.

let keyValue = ref.child("users").child(user.id).child("alerts").key

ref.child("users").child(user.id).child("alerts").child(keyValue).setValue(["autoID" : keyValue])

answered question

1 Answer


Try with

var ref: DatabaseReference!

ref = Database.database().reference()

let keyValue = self.ref.child(name_of_child).childByAutoId().key

posted this

Have an answer?


Please login first before posting an answer.