FIREBASE数据库 - 为子节点存储唯一的密钥(Swift / IOS) [英] FIREBASE Database - Storing Unique Key to child node(Swift/IOS)

查看:222
本文介绍了FIREBASE数据库 - 为子节点存储唯一的密钥(Swift / IOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在调用childByAutoId时存储生成的唯一键。从理论上讲,这将有助于映射对应用程序中稍后需要执行的指定子项的更新或更改。

我对Firebase的分级数据库相当陌生,如果下面描述的方法不正确,请不要犹豫,给出一个替代方案。



我是Firebase的新手,有一个多人游戏应用程序,其结构如下所示:



< pre $ HTGames:{

-KFGX5H3rLSnpPvupakm:{
{
Red:1,
绿色: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
Blue:2,
GameLimit:1,
P1Name:3,
P2Name:2,
P3Name
P1Points:3,
P2Points:2,
P3Points:3,
numberOfPlayers:1.
gameStart:true $




下面的Swift方法调用...

  func startNewGame(){
// Datab ase reference object
let databaseRef = FIRDatabase.database()。reference()

let gameTable:[String:AnyObject] = [Green:,Red: ,蓝色:,GameTypePoint:,P1Name:,P2Name:,P3Name:,P1Points:,P2Points P3Points:,NumOfPlayers:,GameStart:]

//自动生成唯一的ID并在HTGame父母下创建子
databaseRef.child (HTGame)。childByAutoId()。setValue(gameTable)

// **********我想本地保存密钥******** * //
// let key =
}


解决方案你可以从新的孩子那里拿到钥匙,如果你保留一个引用:

$ $ p $ let newRef如果你的数据库中有一个或者多个数据库,你可以使用这个数据库来访问数据库中的所有数据库:$ databaseRef.child(HTGame)。childByAutoId()
newRef.setValue(gameTable)

let key = newRef.key
pre>

有关保存数据指南的更多信息,请参见: https://firebase.google.com/docs/database/ios/save-data#append_to_a_list_of_data


I am attempting to store the generated Unique Key when childByAutoId is called. In theory it would help map updates or changes on a specified child that need to happen later on in my app.

I am fairly new to Firebase's hierarchical database and if the approach described below isn't correct please do not hesitate to give an alternative.

I'm fairly new to Firebase and have a multiplayer app, where my structure currently looks like the following:

"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 method call 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 =
}

解决方案

You can grab the key from the new child, if you keep a reference to it:

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

let key = newRef.key

More on this in the saving data guide: https://firebase.google.com/docs/database/ios/save-data#append_to_a_list_of_data

这篇关于FIREBASE数据库 - 为子节点存储唯一的密钥(Swift / IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆