0

Image of my view controller:

1

This is my code

class AddViewController: UIViewController {

    @IBOutlet weak var questionField: UITextField!
    @IBOutlet weak var correctAnswer: UITextField!
    @IBOutlet weak var optionA: UITextField!
    @IBOutlet weak var optionB: UITextField!


    var ref: FIRDatabaseReference?


    override func viewDidLoad() {
        super.viewDidLoad()
       self.hideKeyboard()
    }

    @IBAction func createQuestion(_ sender: Any) {

         ref = FIRDatabase.database().reference(fromURL: "https://******.firebaseio.com/")
      if questionField.text != "" && correctAnswer.text != "" &&  optionA.text != "" && optionB.text != ""
      {
        self.ref?.child("Questions").setValue(["Question": questionField.text, "CorrectAnswer": correctAnswer.text, "OptionA": optionA.text, "OptionB": optionB.text])
            questionField.text = ""
            correctAnswer.text = ""
            optionA.text = ""
            optionB.text = ""
      }
        else
       {
        print("Missing fields")
        }

}
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

This is my goal in Firebase JSON:

2

My code is working but it replaces(ofc). Is my approach correct in saving questions so that in retrieving it will be easy? Could you give me idea how to save my questions?

AL.
  • 36,815
  • 10
  • 142
  • 281
yveszenne
  • 666
  • 8
  • 18

2 Answers2

0

Since setValue() will override the whole content of Questions node. U should create a child before upload the question to firebase.

ref.child("Questions").childByAutoId().setValue(...)
Peter Cheng
  • 399
  • 1
  • 7
  • yes i know already that it will override. Can you expound more your code how not to override instead create another new question pls – yveszenne Feb 23 '17 at 18:58
  • childByAutoId will generate a new child node under "Questions" node, when u call setValue after childByAutoId(), the question content will store under the generated node.when u upload another question, it will generate another new node to prevent override the previous question content. – Peter Cheng Feb 24 '17 at 02:33
0

First of all you must be using a pretty old version of Firebase if you are creating a database reference like that. For a while now (almost 6 months?) the correct way to create a reference to your database is

var ref: FIRDatabaseReference!

override func viewDidLoad(){
     super.viewDidLoad()
     ref = FIRDatabase.database().reference()
}

And in order for this to work you must download the GoogleService-info.plist file for your firebase project and add it to your Xcode project.


As far as your database structure goes...

What you have in your code is not going to produce what you are aiming for in the picture above. What you have in your code will produce this... JSON based on your current code

In order to produce data structured in the way you have pictured above you need to do this...

ref.child("Questions").child("Question1").setValue(["Question":questionField.text, "CorrectAnswer": correctAnswer.text, "optionA": optionA.text, "optionB": optionB.text])

then for question 2...

ref.child("Questions").child("Question2").setValue(["Question":questionField.text, "CorrectAnswer": correctAnswer.text, "optionA": optionA.text, "optionB": optionB.text])

and then your JSON will look like this ... newly structured JSON

Note that I had to add the "Question" key as a child value under the "Question1" node in order to set the text for that question as its value.

Furthermore, if you are not able to increment Question1 followed by Question2... and so on, then you can use Firebase's method .childByAutoId like this for every question and Firebase will automatically generate a unique child id for that node...

ref.child("Questions").childByAutoId().setValue(["Question":questionField.text, "CorrectAnswer": correctAnswer.text, "optionA": optionA.text, "optionB": optionB.text])

the result will look similar to this... JSON using .childByAutoId

Hope this helps, let me know if you have further questions

MikeG
  • 3,745
  • 1
  • 29
  • 51
  • MikeG, thanks bro it worked as what i want. Could you help me or share little code to retrieve those questions and put it in button pls A BIG Thank YOU for helping me – yveszenne Feb 24 '17 at 04:35
  • @JohnDoe glad I could help, please mark the answer as correct by clicking the check mark. I am happy to show you how to retrieve the data but you ought to open up another question for that since it is a different topic and might be helpful for other people as well! – MikeG Feb 24 '17 at 12:35
  • 1
    This is my question http://stackoverflow.com/questions/42841730/retrieve-from-firebase-to-multiple-buttons-in-swift-3 . Hope you can answer it – yveszenne Mar 17 '17 at 01:56