I have a question bank inside my Firebase Database. So far I am able to extract my questions from Firebase as printed in the debug console print statements below.
How do I pass this question into Realm Database? Currently I am getting a nil value.
Debug Console Print Statements:
Snap (0) {
question = "This is question 1";
}
Hello
This is the dictionary nil
This is the question: nil
Database:
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)
}
}
})
}
}