Swift的AutoID Firebase数据库阵列 [英] Swift Firebase Database Array of AutoID
问题描述
-
我想将所有ChilDBYAutoID的每个图形项都接收到一个双精度数组中.
I would like to receive every graph item from all ChilDBYAutoID's into a double array.
此外,有没有更好的方法来进行计数,因此没有自动ID?例如:
Also, Is there a better way to do this with a count so there is no auto ID? Such as example:
0724 1744 2 745 3 800 . . .
0 724 1 744 2 745 3 800 . . .
我的主要目标是上传许多图形值,而不仅仅是更新一个.然后将图形值检索到Double Array中.
My main goal is to upload many graph values, not just update one. And then retrieve the graph values into a 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结构更改为:-
As far as i understood your problem , You gotta change your JSON structure to :-
genera_Room_Index_Count : 3,
genera_Room_Index : {
1 : 123,
2 : 321,
3 : 565
}
将您的genera_Room_Index_Count
初始化为0;相同的安全规则将应用于genera_Room_Index_Count
节点;然后开始附加值
Initialise your genera_Room_Index_Count
to 0; Same security rules will apply for genera_Room_Index_Count
node; Then start appending the values
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
},
这篇关于Swift的AutoID Firebase数据库阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!