阵中火力地堡/斯威夫特退出OvserveEventType结束后清除 [英] Array is cleared after exiting OvserveEventType closure in Firebase/Swift

查看:231
本文介绍了阵中火力地堡/斯威夫特退出OvserveEventType结束后清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种结构的数据库:

I have a database with this structure:

{
  "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外壳。我已经放了一些印刷()语句中循环,发现我能得到它打印数组作为其追加如果该语句是直接追加code之下,但外}),它只返回空数组。

I am trying to place the itemNames in an array, to be used later in a table. However, it seems to clear my entire array once it exits the observeEventType enclosure. I've placed some print() statements throughout the loop, and found that I can get it to print the array as it appends if the statement is directly beneath the appending code, but outside the }), it only returns an empty array.

    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
        }

    })

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

Can anyone tell me what's happening / how to fix it?

推荐答案

解决它! code。与@周杰伦的回答是低于更新,所有我所要做的就是告诉表格退出循环之前重新加载数据。

Solved it! Code updated with @jay's answer is below, all I had to do was tell the table to reload data before exiting the loop.

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()********

    })

这篇关于阵中火力地堡/斯威夫特退出OvserveEventType结束后清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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