Swift Firebase Database Array of AutoID

2019-09-17 11:50发布

enter image description here enter image description here

  1. I would like to receive every graph item from all ChilDBYAutoID's into a double array.

  2. 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条回答
趁早两清
2楼-- · 2019-09-17 12:08

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 


  },
查看更多
登录 后发表回答