简体   繁体   中英

How can I write multiple data to firebase?

I have a problem about writing multiple data to firebase. I have three different arrays, one int and I have to take uid's child(adress) and uid's name. In simple way I want to make orders node and I have to write all these datas in same node. I tried to make it set value but it takes only the last one other will deleted. I can't find any way to fix it. How can I solve?

Here my code:

func firebaseConfigForUserAndAdress() {
    guard let uid = Auth.auth().currentUser?.uid else { return }

    // these are my arrays
    print(siparisEdilenUrunleriTutanArray)
    print(siparisEdilenFiyatlariTutanArray)
    print(siparisEdilenCountlariTutanArray)



     // values for only try... 
    var values = siparisEdilenUrunleriTutanArray + siparisEdilenFiyatlariTutanArray


    let refOrder = Database.database().reference().child("orders").child("kullanicilar").child(uid)

    refOrder.setValue(values, withCompletionBlock: { (error, ref) in
            if let error = error {
                print("Failed to update database values with error: ", error.localizedDescription)
                return
            }


            //self.dismiss(animated: true, completion: nil)
        })
    refOrder.setValue(["urun sayilari" : self.siparisEdilenCountlariTutanArray])
}

How can I write these datas to firebase?

I'm not sure I understand correctly, but think you may be looking for childByAutoId() . When you call childByAutoId() on a location, Firebase will generate a unique ID under that location where you can then write the data.

For you that'd look something like:

let refOrder = Database.database().reference().child("orders").child("kullanicilar").child(uid)

refOrder.childByAutoId().setValue(values, withCompletionBlock: { (error, ref) in
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM