我希望将所有ChilDBYAutoID中的每个图表项都收到一个双数组。
此外,有没有更好的方法来计数,所以没有自动ID?例如:
0 724 1 744 2 745 3 800 。 。
我的主要目标是上传许多图表值,而不只是更新一个。然后将图形值检索为双数组。
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]
})
答案 0 :(得分:1)
据我了解你的问题,你必须将你的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
},