【问题标题】:FIREBASE Database - Storing Unique Key to child node(Swift/IOS)FIREBASE 数据库 - 将唯一密钥存储到子节点(Swift/IOS)
【发布时间】:2016-12-15 23:06:21
【问题描述】:

我正在尝试在调用 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 =
}

【问题讨论】:

  • 你想要所有的钥匙还是只想要你刚刚存储的钥匙?
  • @Dravidian 就是我存储的那个。

标签: ios database swift firebase firebase-realtime-database


【解决方案1】:

如果您保留对它的引用,您可以从新孩子那里获取钥匙:

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

【讨论】:

  • 非常感谢!你的回答很符合我的需要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2017-11-08
  • 2020-04-22
  • 2023-01-16
  • 1970-01-01
  • 2021-06-25
相关资源
最近更新 更多