(Firebase)在计算用户数组时计算数据库中的用户数量错误 [英] (Firebase) printing wrong number of users in database when counting users in array

查看:199
本文介绍了(Firebase)在计算用户数组时计算数据库中的用户数量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检索Firebase数据库中所有用户的快照。在将我的数据库的用户节点中的所有用户附加到用户数组后,我试图打印用户数(应该是12),并且它表示它是0。 b

我在if语句的大括号处添加了一个断点,它向我显示用户数达到12个。但是,打印此数字的时间为零。我经常在viewDidLoad()方法中调用这个函数,并试图打印用户数量在函数调用之后,除了在函数中打印之外,它仍然是0。

  func getUsers(){

ref.observe(.childAdded,其中:$($)中的{(快照))
$ b let ref = Database.database()。 b如果让dictionary = snapshot.value as?[String:AnyObject] {

let user = User()

user.id = snapshot.key
user .setValuesForKeys(dictionary)
self.users.append(user)
}
$ b $,withCancel:nil)

print(users.count)

$ / code $ / $ p

解决方案

下面。就像Ahmed说的那样:获取数据需要一些时间,因为它是异步的。

  func getUsers(completionHandler:@escaping( ()()){

let ref = Database.database()。reference()。child(users)

ref.observe(.childAdded ,如下:{(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)
}
} )

重写func viewDidLoad(){
super.viewDidLoad()
getUsers(){(count)in
print(count)
}



$ b $ p
$ b

这很简单,我刚刚添加了一个完成处理程序。 / p>

I am retrieving a snapshot of all of the users in my Firebase database. After I append all of my users from my "users" node of my database to an array of users , I attempt to print the user count (which should be 12) and it says it is 0 instead.

I added a breakpoint at the closing brace of the if-statement which shows to me that the user count reaches 12. However, by the time it comes time to print this number, it is zero. Stepping through a bunch of times is just a lot of assembly code that tells me nothing.

I call this function in the viewDidLoad() method and tried to print the users count after this function is called in addition to printing it within the function and it is 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 you go, a fix below. Like Ahmed is saying: it takes some time to get the data since it is 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 is pretty straightforward what is going on, I just added a completion handler.

这篇关于(Firebase)在计算用户数组时计算数据库中的用户数量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆