Обновление данных в Firebase удаляет старого потомка и заменяет его, используя SWIFT - PullRequest
0 голосов
/ 20 марта 2019

Когда я пытаюсь быстро добавить данные в базу данных 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 голосов
/ 20 марта 2019
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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...