无法在堆栈工作中找到答案:如何知道Firebase何时检索数据 [英] Cant find in stack work answer on: How to know when Firebase retrieved data

查看:43
本文介绍了无法在堆栈工作中找到答案:如何知道Firebase何时检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请说明我需要从Firebase检索数据的位置(方法或可能存在的竞争方法).什么是最佳做法?

please explain where (method or maybe exists observe with competition method) I need retrieve data from Firebase. What is best practices?

    var ref:FIRDatabaseReference!

    ref = FIRDatabase.database().reference()

    //filling ShopList by favorite
    ref.child("goods").observeSingleEvent(of:.value, with: {(snapshot) in
        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

            print("my app: 1. Start ObserveSingle")

            for snap in snapshots {

                let g = Good()
                g.barcode = String(describing: snap.childSnapshot(forPath: "barcode").value!)
                g.name = snap.childSnapshot(forPath: "name").value! as! String
                g.price = snap.childSnapshot(forPath: "minprice").value! as! Double
                g.favorite = snap.childSnapshot(forPath: "favorite").value! as! Bool

                if g.favorite {//избранные товары
                    DataService.dataService.dbShopList.append(g)
                }
            }

            print("my app: 2. Observe finished \(DataService.dataService.dbShopList.count)")
        }

    })

    print("my app: 3. observe finished \(DataService.dataService.dbShopList.count)")

如何了解第2步何时何地完成

How to understand when and where finished step 2

在下面调试

    my app: 3. observe finished 0
    my app: 1. Start ObserveSingle
    my app: 2. Observe finished 3

推荐答案

Firebase数据仅在观察之后的闭包内有效.因此,当您致电

Firebase data is only valid within the closure following the observe. So when you call

ref.child("goods").observeSingleEvent(of:.value, with: { 
     //this is the closure where the data is returned and valid
})

这就是陷阱.

print("my app: 3.

将被称为BEFORE

will be called BEFORE

print("my app: 2.

因为代码的执行速度比互联网快!

because code executes faster than the internet!

Firebase返回数据存在延迟,这会导致关闭在 在关闭之外的打印语句后触发.

There is a delay in Firebase returning data which will cause the closure to fire after the print statement that's outside the closure.

在闭包内部使用Firebase数据,因为这是唯一有效的时间.在准备就绪之前尝试访问该数据将导致数据无效,因为在调用闭包之前不会填充变量.

Work with Firebase data INSIDE the closure as that's the only time it's valid. Trying to access that data before it's ready will result in nil data as the variables will not be populated until the closure is called.

根据返回的Firebase数据来习惯设置事件序列需要花费时间.

It takes time to get used to setting up sequences of events based on Firebase data being returned.

这是一些伪代码:

myDataSource = []

Query Firebase for data {
  populate the data source with Firebase data
  reload the tableView
}

don't access the data source here as it won't be ready

我们可以使用您问题中的一段代码将其付诸实践

We can put that into practice using a snippet of code from your question

var ref:FIRDatabaseReference!
ref = FIRDatabase.database().reference()

ref.child("goods").observeSingleEvent(of:.value, with: {(snapshot) in

     print("data from the snapshot is ready to be used")

     for child in snapshot.children {
         var g = Good()
         let snap = child as! FIRDataSnapshot
         let dict = snap.value! as! [String:String]
         g.barcode = dict["barcode"]!
         DataService.dataService.dbShopList.append(g)
     }

     print("All the firebase data was read in, show it in the tableView")
     self.tableView.reloadData()

 })

 print("This will execute before the closure completes so")
 print("  don't do anything with the Firebase data here")

这篇关于无法在堆栈工作中找到答案:如何知道Firebase何时检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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