How do I set my variable correctly to a value on Firebase and not have it reset the progress?

Multi tool use
Multi tool use











up vote
0
down vote

favorite












Everytime I leave this ViewController and then come back the quiz starts back to question 1. I want it to resume from where I left off.



I believe why it keeps resetting to zero is because my initial "questionNumber" is set to 0 but I'm not sure how to set its value to the value in Firebase correctly. I get error after error. I've tried so many different ways but none of them seem to work and resume from where I left off.
Thanks for your help! PLEASE help me!



class CryptoViewController: UIViewController {

var questionList = CryptoBank()
var score = 0
var pickedQuestion = 0
var uid = FIRAuth.auth()?.currentUser?.uid
var questionNumber = 0

@IBOutlet weak var questionViewer: UILabel!
@IBOutlet weak var choiceOne: UIButton!
@IBOutlet weak var choiceTwo: UIButton!
@IBOutlet weak var choiceThree: UIButton!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var questionNumberView: UILabel!
@IBOutlet weak var progressBarView: UIView!

override func viewDidLoad() {
super.viewDidLoad()

choiceOne.titleLabel?.textAlignment = NSTextAlignment.center
choiceTwo.titleLabel?.textAlignment = NSTextAlignment.center
choiceThree.titleLabel?.textAlignment = NSTextAlignment.center
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
update()
}


@IBAction func buttonPressed(_ sender: AnyObject) {
if sender.tag == 1 {
pickedQuestion = 1}
else if sender.tag == 2 {
pickedQuestion = 2}
else if sender.tag == 3 {
pickedQuestion = 3}
checkAnswer()
questionNumber += 1
nextQuestion()
}

func checkAnswer(){
let correctAnswer = questionList.cryptoBank[questionNumber].answer
if pickedQuestion == correctAnswer {
score += 1
}else{
print("Wrong Answer")
}
}

func updateFirebase(){
let ref = FIRDatabase.database().reference()
guard let uid = FIRAuth.auth()?.currentUser!.uid else{
return}
ref.child("Users").child(uid).child("Cryptoquiz").child("Question Number").setValue(questionNumber)
ref.child("Users").child(uid).child("Cryptoquiz").child("Score").setValue(score)
}

func nextQuestion(){
if questionNumber <= 9 {
update()
} else{

scoreLabel.text = "Score: (score)"
let alert = UIAlertController(title: "Done!", message: "Would you like to restart?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
self.startAgain()
})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}}

func update(){
let nextQuest = questionList.cryptoBank[questionNumber]
questionViewer.text = nextQuest.question
choiceOne.setTitle(nextQuest.choice1, for: .normal)
choiceTwo.setTitle(nextQuest.choice2, for: .normal)
choiceThree.setTitle(nextQuest.choice3, for: .normal)
scoreLabel.text = "Score: (score)"
questionNumberView.text = "Question: (questionNumber + 1)"
progressBarView.frame.size.width = (view.frame.size.width/9) * CGFloat(questionNumber + 1)
updateFirebase()

}


}









share|improve this question




























    up vote
    0
    down vote

    favorite












    Everytime I leave this ViewController and then come back the quiz starts back to question 1. I want it to resume from where I left off.



    I believe why it keeps resetting to zero is because my initial "questionNumber" is set to 0 but I'm not sure how to set its value to the value in Firebase correctly. I get error after error. I've tried so many different ways but none of them seem to work and resume from where I left off.
    Thanks for your help! PLEASE help me!



    class CryptoViewController: UIViewController {

    var questionList = CryptoBank()
    var score = 0
    var pickedQuestion = 0
    var uid = FIRAuth.auth()?.currentUser?.uid
    var questionNumber = 0

    @IBOutlet weak var questionViewer: UILabel!
    @IBOutlet weak var choiceOne: UIButton!
    @IBOutlet weak var choiceTwo: UIButton!
    @IBOutlet weak var choiceThree: UIButton!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var questionNumberView: UILabel!
    @IBOutlet weak var progressBarView: UIView!

    override func viewDidLoad() {
    super.viewDidLoad()

    choiceOne.titleLabel?.textAlignment = NSTextAlignment.center
    choiceTwo.titleLabel?.textAlignment = NSTextAlignment.center
    choiceThree.titleLabel?.textAlignment = NSTextAlignment.center
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = .clear
    update()
    }


    @IBAction func buttonPressed(_ sender: AnyObject) {
    if sender.tag == 1 {
    pickedQuestion = 1}
    else if sender.tag == 2 {
    pickedQuestion = 2}
    else if sender.tag == 3 {
    pickedQuestion = 3}
    checkAnswer()
    questionNumber += 1
    nextQuestion()
    }

    func checkAnswer(){
    let correctAnswer = questionList.cryptoBank[questionNumber].answer
    if pickedQuestion == correctAnswer {
    score += 1
    }else{
    print("Wrong Answer")
    }
    }

    func updateFirebase(){
    let ref = FIRDatabase.database().reference()
    guard let uid = FIRAuth.auth()?.currentUser!.uid else{
    return}
    ref.child("Users").child(uid).child("Cryptoquiz").child("Question Number").setValue(questionNumber)
    ref.child("Users").child(uid).child("Cryptoquiz").child("Score").setValue(score)
    }

    func nextQuestion(){
    if questionNumber <= 9 {
    update()
    } else{

    scoreLabel.text = "Score: (score)"
    let alert = UIAlertController(title: "Done!", message: "Would you like to restart?", preferredStyle: .alert)
    let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
    self.startAgain()
    })
    alert.addAction(restartAction)
    present(alert, animated: true, completion: nil)
    }}

    func update(){
    let nextQuest = questionList.cryptoBank[questionNumber]
    questionViewer.text = nextQuest.question
    choiceOne.setTitle(nextQuest.choice1, for: .normal)
    choiceTwo.setTitle(nextQuest.choice2, for: .normal)
    choiceThree.setTitle(nextQuest.choice3, for: .normal)
    scoreLabel.text = "Score: (score)"
    questionNumberView.text = "Question: (questionNumber + 1)"
    progressBarView.frame.size.width = (view.frame.size.width/9) * CGFloat(questionNumber + 1)
    updateFirebase()

    }


    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Everytime I leave this ViewController and then come back the quiz starts back to question 1. I want it to resume from where I left off.



      I believe why it keeps resetting to zero is because my initial "questionNumber" is set to 0 but I'm not sure how to set its value to the value in Firebase correctly. I get error after error. I've tried so many different ways but none of them seem to work and resume from where I left off.
      Thanks for your help! PLEASE help me!



      class CryptoViewController: UIViewController {

      var questionList = CryptoBank()
      var score = 0
      var pickedQuestion = 0
      var uid = FIRAuth.auth()?.currentUser?.uid
      var questionNumber = 0

      @IBOutlet weak var questionViewer: UILabel!
      @IBOutlet weak var choiceOne: UIButton!
      @IBOutlet weak var choiceTwo: UIButton!
      @IBOutlet weak var choiceThree: UIButton!
      @IBOutlet weak var scoreLabel: UILabel!
      @IBOutlet weak var questionNumberView: UILabel!
      @IBOutlet weak var progressBarView: UIView!

      override func viewDidLoad() {
      super.viewDidLoad()

      choiceOne.titleLabel?.textAlignment = NSTextAlignment.center
      choiceTwo.titleLabel?.textAlignment = NSTextAlignment.center
      choiceThree.titleLabel?.textAlignment = NSTextAlignment.center
      self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
      self.navigationController?.navigationBar.shadowImage = UIImage()
      self.navigationController?.navigationBar.isTranslucent = true
      self.navigationController?.view.backgroundColor = .clear
      update()
      }


      @IBAction func buttonPressed(_ sender: AnyObject) {
      if sender.tag == 1 {
      pickedQuestion = 1}
      else if sender.tag == 2 {
      pickedQuestion = 2}
      else if sender.tag == 3 {
      pickedQuestion = 3}
      checkAnswer()
      questionNumber += 1
      nextQuestion()
      }

      func checkAnswer(){
      let correctAnswer = questionList.cryptoBank[questionNumber].answer
      if pickedQuestion == correctAnswer {
      score += 1
      }else{
      print("Wrong Answer")
      }
      }

      func updateFirebase(){
      let ref = FIRDatabase.database().reference()
      guard let uid = FIRAuth.auth()?.currentUser!.uid else{
      return}
      ref.child("Users").child(uid).child("Cryptoquiz").child("Question Number").setValue(questionNumber)
      ref.child("Users").child(uid).child("Cryptoquiz").child("Score").setValue(score)
      }

      func nextQuestion(){
      if questionNumber <= 9 {
      update()
      } else{

      scoreLabel.text = "Score: (score)"
      let alert = UIAlertController(title: "Done!", message: "Would you like to restart?", preferredStyle: .alert)
      let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
      self.startAgain()
      })
      alert.addAction(restartAction)
      present(alert, animated: true, completion: nil)
      }}

      func update(){
      let nextQuest = questionList.cryptoBank[questionNumber]
      questionViewer.text = nextQuest.question
      choiceOne.setTitle(nextQuest.choice1, for: .normal)
      choiceTwo.setTitle(nextQuest.choice2, for: .normal)
      choiceThree.setTitle(nextQuest.choice3, for: .normal)
      scoreLabel.text = "Score: (score)"
      questionNumberView.text = "Question: (questionNumber + 1)"
      progressBarView.frame.size.width = (view.frame.size.width/9) * CGFloat(questionNumber + 1)
      updateFirebase()

      }


      }









      share|improve this question















      Everytime I leave this ViewController and then come back the quiz starts back to question 1. I want it to resume from where I left off.



      I believe why it keeps resetting to zero is because my initial "questionNumber" is set to 0 but I'm not sure how to set its value to the value in Firebase correctly. I get error after error. I've tried so many different ways but none of them seem to work and resume from where I left off.
      Thanks for your help! PLEASE help me!



      class CryptoViewController: UIViewController {

      var questionList = CryptoBank()
      var score = 0
      var pickedQuestion = 0
      var uid = FIRAuth.auth()?.currentUser?.uid
      var questionNumber = 0

      @IBOutlet weak var questionViewer: UILabel!
      @IBOutlet weak var choiceOne: UIButton!
      @IBOutlet weak var choiceTwo: UIButton!
      @IBOutlet weak var choiceThree: UIButton!
      @IBOutlet weak var scoreLabel: UILabel!
      @IBOutlet weak var questionNumberView: UILabel!
      @IBOutlet weak var progressBarView: UIView!

      override func viewDidLoad() {
      super.viewDidLoad()

      choiceOne.titleLabel?.textAlignment = NSTextAlignment.center
      choiceTwo.titleLabel?.textAlignment = NSTextAlignment.center
      choiceThree.titleLabel?.textAlignment = NSTextAlignment.center
      self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
      self.navigationController?.navigationBar.shadowImage = UIImage()
      self.navigationController?.navigationBar.isTranslucent = true
      self.navigationController?.view.backgroundColor = .clear
      update()
      }


      @IBAction func buttonPressed(_ sender: AnyObject) {
      if sender.tag == 1 {
      pickedQuestion = 1}
      else if sender.tag == 2 {
      pickedQuestion = 2}
      else if sender.tag == 3 {
      pickedQuestion = 3}
      checkAnswer()
      questionNumber += 1
      nextQuestion()
      }

      func checkAnswer(){
      let correctAnswer = questionList.cryptoBank[questionNumber].answer
      if pickedQuestion == correctAnswer {
      score += 1
      }else{
      print("Wrong Answer")
      }
      }

      func updateFirebase(){
      let ref = FIRDatabase.database().reference()
      guard let uid = FIRAuth.auth()?.currentUser!.uid else{
      return}
      ref.child("Users").child(uid).child("Cryptoquiz").child("Question Number").setValue(questionNumber)
      ref.child("Users").child(uid).child("Cryptoquiz").child("Score").setValue(score)
      }

      func nextQuestion(){
      if questionNumber <= 9 {
      update()
      } else{

      scoreLabel.text = "Score: (score)"
      let alert = UIAlertController(title: "Done!", message: "Would you like to restart?", preferredStyle: .alert)
      let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
      self.startAgain()
      })
      alert.addAction(restartAction)
      present(alert, animated: true, completion: nil)
      }}

      func update(){
      let nextQuest = questionList.cryptoBank[questionNumber]
      questionViewer.text = nextQuest.question
      choiceOne.setTitle(nextQuest.choice1, for: .normal)
      choiceTwo.setTitle(nextQuest.choice2, for: .normal)
      choiceThree.setTitle(nextQuest.choice3, for: .normal)
      scoreLabel.text = "Score: (score)"
      questionNumberView.text = "Question: (questionNumber + 1)"
      progressBarView.frame.size.width = (view.frame.size.width/9) * CGFloat(questionNumber + 1)
      updateFirebase()

      }


      }






      swift firebase restart saving-data






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 7:14









      PradyumanDixit

      1,7971718




      1,7971718










      asked Nov 7 at 6:03









      Tim Kruger

      591235




      591235
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can save your progress in UserDefaults in viewDidDisappear() like this:



          UserDefaults.standard.set(questionNumber, forKey: "questionNumber")


          And then every time you open your view, in viewDidLoad() or in viewDidAppear() you update your questionNumber like this:



          let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          questionNumber = questionNumberSaved


          Then after doing this you call your updateFirebase() method






          share|improve this answer





















          • I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
            – Tim Kruger
            Nov 7 at 16:41












          • Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
            – DionizB
            Nov 7 at 16:43










          • I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
            – Tim Kruger
            Nov 7 at 16:59






          • 1




            What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
            – DionizB
            Nov 7 at 17:00






          • 1




            Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
            – DionizB
            Nov 7 at 18:31











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184310%2fhow-do-i-set-my-variable-correctly-to-a-value-on-firebase-and-not-have-it-reset%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You can save your progress in UserDefaults in viewDidDisappear() like this:



          UserDefaults.standard.set(questionNumber, forKey: "questionNumber")


          And then every time you open your view, in viewDidLoad() or in viewDidAppear() you update your questionNumber like this:



          let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          questionNumber = questionNumberSaved


          Then after doing this you call your updateFirebase() method






          share|improve this answer





















          • I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
            – Tim Kruger
            Nov 7 at 16:41












          • Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
            – DionizB
            Nov 7 at 16:43










          • I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
            – Tim Kruger
            Nov 7 at 16:59






          • 1




            What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
            – DionizB
            Nov 7 at 17:00






          • 1




            Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
            – DionizB
            Nov 7 at 18:31















          up vote
          1
          down vote













          You can save your progress in UserDefaults in viewDidDisappear() like this:



          UserDefaults.standard.set(questionNumber, forKey: "questionNumber")


          And then every time you open your view, in viewDidLoad() or in viewDidAppear() you update your questionNumber like this:



          let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          questionNumber = questionNumberSaved


          Then after doing this you call your updateFirebase() method






          share|improve this answer





















          • I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
            – Tim Kruger
            Nov 7 at 16:41












          • Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
            – DionizB
            Nov 7 at 16:43










          • I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
            – Tim Kruger
            Nov 7 at 16:59






          • 1




            What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
            – DionizB
            Nov 7 at 17:00






          • 1




            Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
            – DionizB
            Nov 7 at 18:31













          up vote
          1
          down vote










          up vote
          1
          down vote









          You can save your progress in UserDefaults in viewDidDisappear() like this:



          UserDefaults.standard.set(questionNumber, forKey: "questionNumber")


          And then every time you open your view, in viewDidLoad() or in viewDidAppear() you update your questionNumber like this:



          let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          questionNumber = questionNumberSaved


          Then after doing this you call your updateFirebase() method






          share|improve this answer












          You can save your progress in UserDefaults in viewDidDisappear() like this:



          UserDefaults.standard.set(questionNumber, forKey: "questionNumber")


          And then every time you open your view, in viewDidLoad() or in viewDidAppear() you update your questionNumber like this:



          let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          questionNumber = questionNumberSaved


          Then after doing this you call your updateFirebase() method







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 9:16









          DionizB

          707310




          707310












          • I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
            – Tim Kruger
            Nov 7 at 16:41












          • Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
            – DionizB
            Nov 7 at 16:43










          • I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
            – Tim Kruger
            Nov 7 at 16:59






          • 1




            What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
            – DionizB
            Nov 7 at 17:00






          • 1




            Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
            – DionizB
            Nov 7 at 18:31


















          • I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
            – Tim Kruger
            Nov 7 at 16:41












          • Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
            – DionizB
            Nov 7 at 16:43










          • I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
            – Tim Kruger
            Nov 7 at 16:59






          • 1




            What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
            – DionizB
            Nov 7 at 17:00






          • 1




            Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
            – DionizB
            Nov 7 at 18:31
















          I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
          – Tim Kruger
          Nov 7 at 16:41






          I did like you said and tried a few other things but it's still always resetting to 0 when returning to the viewController. Thanks for trying to help....I appreciate it alot.
          – Tim Kruger
          Nov 7 at 16:41














          Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
          – DionizB
          Nov 7 at 16:43




          Where did you put loading scores code? And where did you try to save questionNumber? Did you print question number after you loaded it from user defaults?
          – DionizB
          Nov 7 at 16:43












          I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
          – Tim Kruger
          Nov 7 at 16:59




          I update the questionNumber var in Firebase, in the updateFirebase func I created. I've tried to print out statements in various places a to see when it resets to 0. For what what I understand it switches to 0 not when I go back to the quiz choice page but when I return to this page. Firebase's data reverts back to 0 when I click on the quiz I want to take. The inital value when I set up questionNumber is 0 but everything I try to get it set to the value in Firebase doesn't work. I've tried to reset it in viewDidLoad to the firebase value but it never works. It still resets to 0
          – Tim Kruger
          Nov 7 at 16:59




          1




          1




          What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          – DionizB
          Nov 7 at 17:00




          What does it print after this line let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
          – DionizB
          Nov 7 at 17:00




          1




          1




          Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
          – DionizB
          Nov 7 at 18:31




          Are you sure you did save question number in UserDefaults? Try placing this UserDefaults.standard.set(questionNumber, forKey: "questionNumber") inside your updateFirebase() method
          – DionizB
          Nov 7 at 18:31


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184310%2fhow-do-i-set-my-variable-correctly-to-a-value-on-firebase-and-not-have-it-reset%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          2J,hfC83Q HwhVk Y MM
          UU6R j h 1vgUgRugu6ZMcN7PPRkJ8iixzHY9j36AdlQNdycvlSAl2O Vlo Gses8MqYtvnSd 3tPS

          Popular posts from this blog

          横浜市

          Rostock

          Europa