简体   繁体   中英

Swift Firebase Get childByAutoId Key When Setting

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").childByAutoId().key

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

used keyValue in replacement of childByAutoId()

Try with

var ref: DatabaseReference!

ref = Database.database().reference()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM