Swift Firebase Get childByAutoId Key When Setting

房东的猫 提交于 2020-07-16 08:07:11

问题


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()


回答1:


Try with

var ref: DatabaseReference!

ref = Database.database().reference()

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


来源:https://stackoverflow.com/questions/53200763/swift-firebase-get-childbyautoid-key-when-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!