Reputation: 283
I've read a lot of articles on this website, which were focusing on writing data to the Firebase Database. However, none of them could solve my problem.
I've created an object with an autoID, however to delete it later on, I need the autoID I've created the object with. For that reason I need to save the autoID to that object. It just doesn't work out.
Here is the code:
@IBAction func Save(_ sender: Any) {
let Town = Stadt.text
let surface = Untergrund.text
let size = Groesse.text
let numberof = AnzToreKoerbe.text
let laenge = longitudeLabel.text
let breite = latitudeLabel.text
let street = Strasse.text
let Art = Platzart.text
var PlatzID = ""
var upload : [String : AnyObject] = ["Platzart": Art as AnyObject,
"Laengengrad" : laenge as AnyObject,
"Breitengrad" : breite as AnyObject,
"Stadt" : Town as AnyObject,
"Strasse" : street as AnyObject,
"Untergrund" : surface as AnyObject,
"Groesse" : size as AnyObject,
"AnzahlToreKoerbe" : numberof as AnyObject,
"PlatzID" : PlatzID as AnyObject]
ref?.child("Sportplatz").childByAutoId().setValue(upload)
PlatzID = (ref?.key)!
ref?.updateChildValues(["PlatzID" : PlatzID as AnyObject])
I've tried to save the key, as some of the articles recommended and then update the child "PlatzID" with that key. When I create a new object, the PlatzID ist still empty.
Thanks for your help.
Upvotes: 1
Views: 2216
Reputation: 717
Not sure if this can help you + I am a bit late with the answer, but you can get and save autoID like this:
let randomID = Database.database().reference().childByAutoId()
print(randomID.key)
Upvotes: 9