0

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:

enter image description here Code:

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)

                }

            }

        })

    }
}
Captain725
  • 73
  • 1
  • 7
  • 1
    You cannot 'pass data' from Firebase to Realm. However, Firebase data can be read and then you can take that data and store it in Realm object, and then write the object to Realm. It looks like the `Question` object is a Realm object with a property .question. So is the question you actually have: `How do I read data from the Firebase Real Time Database and get the data from the child nodes'? If so, did you go through the Firebase Getting Started Guide [Read Data](https://firebase.google.com/docs/database/ios/read-and-write#read_data_once) as that's covered. – Jay May 26 '20 at 16:55
  • Hey Jay, yes reading from Firebase & then writing to Realm was the intent. I looked at your reference & it did help by removing the for loop & casting dictionary as an NSDictionary – Captain725 May 27 '20 at 06:08
  • Does that mean the issue is resolved? If not, update and clarify the question and we'll take a look. – Jay May 27 '20 at 12:53
  • Hey Jay, yes it is resolved – Captain725 Jun 01 '20 at 16:14
  • I’m voting to close this question because the OP was able to resolve the issue on their own. – Jay Jun 01 '20 at 16:41
  • Hey Jay, would you be able to help me with another Realm question? https://stackoverflow.com/questions/62247520/how-to-apply-multiple-filters-to-realm-database-results – Captain725 Jun 08 '20 at 04:46

0 Answers0