首页 > 解决方案 > 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()

标签: iosswiftfirebasefirebase-realtime-database

解决方案


尝试

var ref: DatabaseReference!

ref = Database.database().reference()

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

推荐阅读