读取数据 Firebase 分配值 [英] Read Data Firebase assign value

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

问题描述

我想将我在 Firebase 数据库中读取的数据分配给我在代码中给出的值.正在我给出的代码中读取数据,但未分配值.我想分配 deviceroom1、deviceRoom2、deviceRoom3、deviceRoom4.

I want to assign the data I have read in the Firebase database to the values I have given in the code. The data is being read in the code I have given, but the values are not being assigned. I want to assign deviceroom1, deviceRoom2, deviceRoom3, deviceRoom4.

    var deviceRoom1: String = ""
    var deviceRoom2: String = ""
    var deviceRoom3: String = ""
    var deviceRoom4: String = ""
    var deviceRoom5: String = ""

 func fetchDevies(){

        let ref = Database.database().reference().child(self.chip1InfoString!).child("Rooms")

        ref.observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary

            if let deviceRooms1 = value?["Rooms1"] as? String  {
                self.deviceRoom1 = deviceRooms1

            }
            if let deviceRooms2 = value?["Rooms2"] as? String {
                self.deviceRoom2 = deviceRooms2

            }
            if let deviceRooms3 = value?["Rooms3"] as? String {
                self.deviceRoom3 = deviceRooms3

            }
            if let deviceRooms4 = value?["Rooms4"] as? String {
                self.deviceRoom4 = deviceRooms4

            }
            if let deviceRooms5 = value?["Rooms5"] as? String {
                self.deviceRoom5 = deviceRooms5

            }
        }) { (error) in
            print(error.localizedDescription)
        }

                let items = [deviceRoom1, deviceRoom2, deviceRoom3, deviceRoom4, deviceRoom5]

推荐答案

这个问题是 Firebase 是异步的,所以你需要给 Firebase 时间来获取和返回请求的数据.即数据仅在闭包内有效.

This issue is that Firebase is asynchronous so you need to give Firebase time to fetch and return the requested data. i.e. the data is only valid within the closure.

您的代码在闭包外使用 let items = ... 分配值,并且该代码将在返回数据之前执行.像这样在闭包中移动赋值(代码为简洁起见)

Your code is assigning values with let items = ... outside the closure and that code will execute way before the data is returned. Move the assignment inside like closure like this (code is shortened for brevity)

func fetchDevies() {
    let ref = Database.database().reference().child("some_node").child("Rooms")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        let value = snapshot.value as? NSDictionary

        if let deviceRooms1 = value?["Rooms1"] as? String  {
            self.deviceRoom1 = deviceRooms1
        }
        .
        .
        .
        let items = [self.deviceRoom1, self.deviceRoom2, self.deviceRoom3, self.deviceRoom4, self.deviceRoom5]
        print(items)
    })

    //code here will run before Firebase has returned data and populated the vars

另外,注意 var 名称.如果您正在访问类 var,请始终使用 self 引用它.像 self.deviceRoom1

Also, be careful with var names. If you are accessing a class var, always refer to it with self. like self.deviceRoom1

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

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