FIREBASE数据库 – 为子节点存储唯一的密钥(Swift / IOS)
我正在尝试存储生成的唯一密钥时调用childByAutoId。 从理论上讲,这将有助于映射对应用程序中稍后需要执行的指定子项的更新或更改。
我对Firebase的分级数据库相当陌生,如果下面描述的方法不正确,请不要犹豫,给一个替代scheme。
我对Firebase相当陌生,有一个多人游戏应用程序,我的结构目前看起来如下所示:
"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 }}} }
下面的Swift方法调用…
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 = }
你可以从新的孩子那里拿到钥匙,如果你有一个参考:
let newRef = databaseRef.child("HTGame").childByAutoId() newRef.setValue(gameTable) let key = newRef.key
有关更多信息,请参阅保存数据指南: https : //firebase.google.com/docs/database/ios/save-data#append_to_a_list_of_data