【问题标题】:Array is cleared after exiting OvserveEventType closure in Firebase/Swift在 Firebase/Swift 中退出 OvserveEventType 闭包后,数组被清除
【发布时间】:2016-05-08 16:16:30
【问题描述】:

我有一个具有这种结构的数据库:

{
  "users" : {
    "8eee4af2-7a02-4c5c-9c2a-5a50623bd681" : {
      "menu" : {
        "-KHG5aYVJM4iDQqA0EQS" : {
          "itemDescription" : "Description 1",
          "itemName" : "Item 1",
          "itemPrice" : "Price 1"
        },
        "-KHG5eJUAUbtXZLEF2Fb" : {
          "itemDescription" : "Description 2",
          "itemName" : "Item 2",
          "itemPrice" : "Price 2"
        },
        "-KHG5hsYhJTrzbqBUPOO" : {
          "itemDescription" : "Description 3",
          "itemName" : "Item 3",
          "itemPrice" : "Price 3"
        },
        "-KHG5lDw-uQXrKobPDlC" : {
          "itemDescription" : "Description 4",
          "itemName" : "Item 4",
          "itemPrice" : "Price 4"
        },
      },
    },
  },
},

我正在尝试将 itemNames 放在一个数组中,以便稍后在表中使用。但是,一旦它退出 observeEventType 外壳,它似乎会清除我的整个数组。我在整个循环中放置了一些 print() 语句,发现如果语句直接位于附加代码下方但在 } 之外,我可以让它打印数组,因为它附加,但它只返回一个空数组。

    let menuItems = ref.childByAppendingPath("users/\(ref.authData.uid)/menu")
    menuItems.observeSingleEventOfType(.Value, withBlock: { snapshot in
        for id in snapshot.children.allObjects as! [FDataSnapshot] {


            let itemName = ref.childByAppendingPath("users/\(ref.authData.uid)/menu/\(id.key)/itemName")
            itemName.observeEventType(.Value, withBlock:{ snapshot in

                self.itemNames.append(snapshot.value as! String)
                print(self.itemNames)
                //This print returns an array full of itemNames

            })

          print(self.itemNames)
          //This print returns nil
        }

    })

谁能告诉我发生了什么/如何解决它?

【问题讨论】:

  • itemNames 已经在 menuItems 返回的值中。您遍历快照,每个 id 将是一个单独的孩子,您可以从 let name = id.value["itemName"] 获取名称。为什么在第一个块内需要另一个观察?
  • 嗯,所以我用 name = id.value["itemName"] 替换了我的内部观察。不过,当我退出该块时,它仍在清除。
  • 这是我的数组的设置方式: var itemNames:[String] = [] var itemPrices:[String] = [] var itemDescriptions:[String] = []

标签: arrays swift firebase


【解决方案1】:

解决了!使用@jay 的答案更新的代码如下,我所要做的就是告诉表格在退出循环之前重新加载数据。

let menuItems = ref.childByAppendingPath("users/\(ref.authData.uid)/menu")
    menuItems.observeEventType(.Value, withBlock: { snapshot in

        //each time I reload this data after submitting a new menu item, I have to clear itemNames, otherwise it adds duplicate items.
        self.itemNames = [String]()
        self.itemPrices = [String]()
        self.itemDescriptions = [String]()

        for id in snapshot.children.allObjects as! [FDataSnapshot] {

            let name = id.value["itemName"] as! String
            self.itemNames.append(name)

            let price = id.value["itemPrice"] as! String
            self.itemPrices.append(price)

            let description = id.value["itemDescription"] as! String
            self.itemDescriptions.append(description)



        }

        //Solution below.  MenuTable is the table all of this is being displayed in
        ******self.menuTable.reloadData()********

    })

【讨论】:

    猜你喜欢
    • 2017-03-17
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2017-07-18
    • 2020-12-03
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多