在Firebase中更新数据会删除旧的子节点,并使用SWIFT替换它

时间:2019-03-20 05:44:20

标签: ios swift firebase firebase-realtime-database

当我尝试快速将数据添加到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)

    } 

这是Firebase数据结构: enter image description here

我在这里做错了什么?任何帮助,将不胜感激!谢谢!

1 个答案:

答案 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
}