问题
I would like to receive every graph item from all ChilDBYAutoID's into a double array.
Also, Is there a better way to do this with a count so there is no auto ID? Such as example:
0 724 1 744 2 745 3 800 . . .
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]
})
回答1:
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
}
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)
})
}
})
})
}
Security rules
"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
},
来源:https://stackoverflow.com/questions/42918543/swift-firebase-database-array-of-autoid