Как извлечь данные из Firebase и добавить их в базу данных Realm - PullRequest
0 голосов
/ 26 мая 2020

У меня есть банк вопросов в моей базе данных Firebase. На данный момент я могу извлечь свои вопросы из Firebase, как указано в инструкциях печати консоли отладки ниже.

Как передать этот вопрос в базу данных Realm? В настоящее время я получаю значение nil.

Заявления для печати консоли отладки:

Snap (0) {
    question = "This is question 1";
}

Hello

This is the dictionary nil

This is the question: nil

База данных:

enter image description here Код:

import UIKit
import RealmSwift
import FirebaseDatabase

class AnswerViewController: UIViewController {

let realmOne = try! Realm()

override func viewDidLoad() {
    super.viewDidLoad()

    grabData()

    }

    func grabData() {

        var ref = Database.database().reference()
        ref.child("0").observe(.value, with: {
            snapshot in
            print (snapshot)

            for snap in snapshot.children.allObjects as! [DataSnapshot] {
                print ("Hello")
                let dictionary = snap.value as? [String : AnyObject]
                print ("This is the dictionary \(dictionary)")

                var question = dictionary?["question"] as? String
                print ("This is the question: \(question)")

                var questionToAdd = Question()
                questionToAdd.question = "Supposed to be question, but it is currently nil"

                try! self.realmOne.write {
                    self.realmOne.add(questionToAdd)

                }

            }

        })

    }
}
...