当我尝试快速将数据添加到Firebase数据库时,而不是添加新的子项,而是删除了旧的子项并替换了它。这是我的快速代码:
func wordsAdd() {
newRef = Database.database().reference()
newRef?.child("wordList").child("\(wordTypePicker.selectedRow(inComponent: 0))").child("category").setValue(wordTypeSelection)
newRef?.child("wordList").child("\(wordTypePicker.selectedRow(inComponent: 0))").child("categoryID").setValue(wordTypePicker.selectedRow(inComponent: 0))
newRef?.child("wordList").child("\(wordTypePicker.selectedRow(inComponent: 0))").child("words").setValue(wordListArray)
}
我在这里做错了什么?任何帮助,将不胜感激!谢谢!
答案 0 :(得分:0)
func addWords() {
let ref = Database.database().reference(withPath: "wordList")
let word: NSArray = [wordTextField.text!] // Watch out with force unwrapping here better to use guard
let post =
[
"category": wordTypeSelection
"categoryID": categoryID
"words" : word
] as [String : Any]
print(post)
ref.child("0").setValue(post) // If you know in which node you have to put the data
// or
ref.updateChildValues(post) // To update every child
}