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
Post a Comment