![](/img/trans.png)
[英]Retrieve last child from child node firebase realtime database swift 4
[英]FIREBASE Database - Storing Unique Key to child node(Swift/IOS)
我试图在调用childByAutoId时存储生成的唯一键 。 理论上,它可以帮助映射需要在我的应用程序中稍后发生的指定子项的更新或更改。
我对Firebase的分层数据库还不熟悉,如果下面描述的方法不正确,请不要犹豫,给出替代方案。
我对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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.