簡體   English   中英

Swift的AutoID Firebase數據庫陣列

[英]Swift Firebase Database Array of AutoID

在此處輸入圖片說明 在此處輸入圖片說明

  1. 我想將所有ChilDBYAutoID的每個圖形項都接收到一個雙精度數組中。

  2. 另外,有沒有更好的方法來進行計數,所以沒有自動ID? 例如:

    0 724 1 744 2 745 3 800

我的主要目標是上傳許多圖形值,而不僅僅是更新一個。 然后將圖形值檢索到Double Array中。

func uploadToFirebase(){

   //Firebase Initialization
    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()

    ref.child("general_room_index").childByAutoId().setValue(["graph_value": totalCountY])

}

databaseRef.child("general_room_index").observeSingleEventOfType(.Value, withBlock: { (snapshot) in

  snapshot.value!["medals"] as! [Double]

    })

據我了解您的問題,您必須將JSON結構更改為:-

genera_Room_Index_Count : 3,

genera_Room_Index : {
       1 : 123,
       2 : 321, 
       3 : 565
          }

將您的genera_Room_Index_Count初始化為0; 相同的安全規則將應用於genera_Room_Index_Count節點; 然后開始附加值

 func uploadToFirebase(your_Value : Int){   // your_Value is your graph value as parameter

   FIRDatabase.database().reference().child("genera_Room_Index_Count").observeSingleEvent(of: .value, with: {(Counts) in

        let count = Counts.value as! Int + 1

        print(count)

        FIRDatabase.database().reference().child("genera_Room_Index").child(String(describing: count)).setValue(your_Value, withCompletionBlock: { (Err, Ref) in

            print(Err ?? "No error")
            print(Ref)

            if Err == nil{

                FIRDatabase.database().reference().child("genera_Room_Index_Count").runTransactionBlock({ (currentData) -> FIRTransactionResult in

                    var value = currentData.value as? Int

                    if value == nil {
                        value = 0
                    }

                    currentData.value = value! + 1

                    return FIRTransactionResult.success(withValue: currentData)

                })
            }
        })
    })
}

安全規則

"genera_Room_Index" :{

     ".read" : "true",    // Or whatever your authentication flowchart might be
    ".write" : "true",    // Or whatever your authentication flowchart might be


  },

  "genera_Room_Index_Count" :{

     ".read" : "true",     // Or whatever your authentication flowchart might be
    ".write" : "true",     // Or whatever your authentication flowchart might be 


  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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