ios - Value of variable not changing in observeSingleEvent -


the variable num changes want inside observesingleevent, after changes empty value. how value of num change??

   var num = int()    firdatabase.database().reference().child("menus/day1").observesingleevent(of: .value, with: {(snap) in      print(snap)      if let snapdict = snap.value as? [string:anyobject]{          self.num = snapdict["date"] as! int         print(num)         //returns number want      }     })      print(num)     //returns empty value      if num == 5 {         print("number 5")     else {         print("number not 5")         //goes      } 

any work need values coming firebase must done within completion handler (the with part of method call). use/manipulate num value firebase, need use within completion handler.

var num = int()  firdatabase.database().reference().child("menus/day1").observesingleevent(of: .value, with: {(snap) in      print(snap)      if let snapdict = snap.value as? [string:anyobject]{          self.num = snapdict["date"] as! int          if self.num == 5 {             print("number 5")         else {             print("number not 5")         }     } }) 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -