ios - (Firebase) printing wrong number of users in database when counting users in array -


i retrieving snapshot of of users in firebase database. after append of users "users" node of database array of users , attempt print user count (which should 12) , says 0 instead.

i added breakpoint @ closing brace of if-statement shows me user count reaches 12. however, time comes time print number, zero. stepping through bunch of times lot of assembly code tells me nothing.

i call function in viewdidload() method , tried print users count after function called in addition printing within function , still 0.

func getusers() {      let ref = database.database().reference().child("users")      ref.observe(.childadded, with: { (snapshot) in         if let dictionary = snapshot.value as? [string: anyobject] {              let user = user()              user.id = snapshot.key             user.setvaluesforkeys(dictionary)                             self.users.append(user)         }      }, withcancel: nil)      print(users.count) } 

here go, fix below. ahmed saying: takes time data since async.

func getusers(completionhandler:@escaping (int) -> ()){      let ref = database.database().reference().child("users")      ref.observe(.childadded, with: { (snapshot) in         if let dictionary = snapshot.value as? [string: anyobject] {              let user = user()              user.id = snapshot.key             user.setvaluesforkeys(dictionary)             self.users.append(user)             completionhandler(self.users.count)         }     })  override func viewdidload() {     super.viewdidload()     getusers(){ (count) in         print(count)     } } 

it pretty straightforward going on, added completion handler.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -