FIREBASE Database - Store a unique key for a child node (Swift / IOS)

I am trying to save the generated unique key when calling childByAutoId. Theoretically, this would help to update the maps or changes on the specified child that should happen later in my application.

I am new to the Firebase hierarchical database, and if the approach described below is incorrect, feel free to give an alternative.

I am new to Firebase and have a multi-user application in which my structure looks like this:

"HTGames" : { "-KFGX5H3rLSnpPvupakm" : { { "Red" : 1, "Green" : 3, "Blue" : 2, "GameLimit" : 1, "P1Name" : 3, "P2Name" : 2, "P3Name" : 1, "P1Points" : 3, "P2Points" : 2, "P3Points": 3, "numberOfPlayers" : 1. "gameStart": true } }, "-X5Hhgljh3rLSnpPvum" : { { "Red" : 1, "Green" : 3, "Blue" : 2, "GameLimit" : 1, "P1Name" : 3, "P2Name" : 2, "P3Name" : 1, "P1Points" : 3, "P2Points" : 2, "P3Points": 3, "numberOfPlayers" : 1. "gameStart": true }}} } 

call Swift method below ...

 func startNewGame(){ //Database reference object let databaseRef = FIRDatabase.database().reference() let gameTable : [String : AnyObject] = ["Green": "", "Red": "", "Blue": "", "GameTypePoint": "","P1Name": "", "P2Name": "", "P3Name": "", "P1Points": "","P2Points": "", "P3Points": "","NumOfPlayers": "","GameStart": ""] //Auto generates unique ID and creates child under "HTGame" parent databaseRef.child("HTGame").childByAutoId().setValue(gameTable) //**********I would like to store key locally here*********// //let key = } 
+7
database ios swift firebase firebase-database
source share
1 answer

You can take the key to a new child if you keep a link to it:

 let newRef = databaseRef.child("HTGame").childByAutoId() newRef.setValue(gameTable) let key = newRef.key 

See the data retention guide for more details: https://firebase.google.com/docs/database/ios/save-data#append_to_a_list_of_data

+9
source share

All Articles