簡體   English   中英

FIREBASE數據庫 - 存儲子節點的唯一鍵(Swift / IOS)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM