从Firebase数据库读取数据时出现问题 [英] Issues reading data from Firebase Database

查看:113
本文介绍了从Firebase数据库读取数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在从数据库中读取数据,当我打印单个变量时,它们会正确打印出来.但是似乎数据拒绝追加到数组.有人知道为什么吗?我完全不知道.

Okay I am reading from a database and when I print the individual variables they print out correctly. However it seems like the data refuses to append to the array. Anyone know why? I can't figure it out at all.

let commuteBuilder = Commutes()

    Database.database().reference().child("Users").child(user).child("Trips").observe(DataEventType.childAdded, with: { (snapshot) in


        //print(snapshot)

        if let dict = snapshot.value as? NSDictionary {
            commuteBuilder.distance = dict["Distance"] as! Double
            commuteBuilder.title = dict["TripName"] as! String
            commuteBuilder.transportType = (dict["Transport"] as? String)!

        }

        commuteArray.append(commuteBuilder)
    })
    print("helper")
    print(commuteArray.count)
    return commuteArray

推荐答案

数据 已正确添加到数组,而不是在打印数组内容时添加.

The data is correctly added to the array, just not at the time that you print the array's contents.

如果您这样更改代码,则可以看到以下内容:

If you change the code like this, you can see this:

let commuteBuilder = Commutes()

Database.database().reference().child("Users").child(user).child("Trips").observe(DataEventType.childAdded, with: { (snapshot) in

    if let dict = snapshot.value as? NSDictionary {
        commuteBuilder.distance = dict["Distance"] as! Double
        commuteBuilder.title = dict["TripName"] as! String
        commuteBuilder.transportType = (dict["Transport"] as? String)!

    }

    commuteArray.append(commuteBuilder)
    print("added one, now have \(commuteArray.count)")
})
print("returning \(commuteArray.count)")
return commuteArray

您会看到它打印出这样的内容:

You'll see it print something like this:

返回0

添加了一个,现在有1个

added one, now have 1

添加了一个,现在有2个

added one, now have 2

这可能不是您期望的输出.但是它按预期工作. Firebase从其数据库异步加载数据.不会阻塞代码,而是让线程继续运行(以便用户可以继续使用该应用程序),并在有新数据可用时回调回到您传递给observe的代码块.

This is likely not the output you expected. But it is working as intended. Firebase loads data from its database asynchronously. Instead of blocking your code, it lets the thread continue (so the user can continue using the app) and instead calls back to the code block you passed to observe when new data is available.

这意味着,当这段代码返回数组时,它仍然为空,但是稍后会在它们进入时添加项目.这意味着您无法以尝试的方式从函数返回数据.

This means that by the time this code returns the array it is still empty, but it later adds items as they come in. This means that you cannot return data from a function in the way you are trying.

我发现最容易改变我对代码的思考方式.代替首先获取数据,然后打印",将其构架为开始获取数据".当数据返回时,将其打印出来."

I find it easiest to change my way of thinking about code. Instead of "First get the data, then print it", I frame it as "Start getting the data. When data comes back, print it".

在上面的代码中,我通过将打印计数回调块中的代码进行了操作.除了这样做,您还可以创建自己的回调,在Swift中称为完成处理程序 closure .您可以在本文使用Swift 或苹果文档中的回调函数语法.

In the code above, I did this by moving the code that prints the count into the callback block. Instead of doing this, you can also create your own callback, which is called a completion handler or closure in Swift. You can find examples in this article, this article, this question Callback function syntax in Swift or of course in Apple's documentation.

这篇关于从Firebase数据库读取数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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