问题
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