【问题标题】:How to Firebase retrieve data below UID and auto ID In Swift如何在 Swift 中 Firebase 检索 UID 和自动 ID 以下的数据
【发布时间】:2019-05-17 23:25:41
【问题描述】:

[在此处输入图像描述][1]我正在尝试从数据库中检索特定数据。我想读取product_list中的所有数据

我的数据库

这是我尝试的,但是当我打印 listName 时。它显示像这样 listName = nil

 let refList = Database.database().reference().child("Users/Sellers")
        refList.observe(DataEventType.value, with:{(snapshot) in
            if snapshot.childrenCount>0{
                self.listProduct.removeAll()

                for lists in snapshot.children.allObjects as! [DataSnapshot]{
                    let userList = lists.value as? [String: AnyObject]
                    let listName = userList?["name"]
                    let listDetail = userList?["detail"]
                    let listPrice = userList?["price"]
                    print("key = \(listName)")

                    let list = ListModel(name: listName as! String?, detail: listDetail as! String?, price: listPrice as! String?)

                    self.listProduct.append(list)
                }
                self.tableList.reloadData()
            }

        })
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

        let list: ListModel
        list = listProduct[indexPath.row]

        cell.lblName.text = list.name
        cell.lblDetail.text = list.detail
        cell.lblPrice.text = list.price

        return cell
    }

它有 4 个我想要检索的东西

(详细信息、名称、价格和 product_image_url)

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    现在您的代码正在循环/Users/Sellers 的子节点,这是每个用户的节点。您似乎想要做的是在每个用户的 product_list 属性下包含所有节点,这需要一个额外的嵌套循环:

    let refList = Database.database().reference().child("Users/Sellers")
    
    refList.observe(DataEventType.value, with:{(snapshot) in
        if snapshot.childrenCount>0{
            self.listProduct.removeAll()
    
            for user in snapshot.children.allObjects as! [DataSnapshot]{
                let productList = user.childSnapshot(forPath: "product_list")
                for product in productList.children.allObjects as! [DataSnapshot]{
                  let userList = product.value as? [String: AnyObject]
                  let listName = userList?["name"]
                  let listDetail = userList?["details"] // NOTE: also fixed a typo here
                  let listPrice = userList?["price"]
                  print("key = \(listName)")
    
                  let list = ListModel(name: listName as! String?, detail: listDetail as! String?, price: listPrice as! String?)
    
                  self.listProduct.append(list)
    
                }
            }
            self.tableList.reloadData()
        }
    
    })
    

    【讨论】:

    • 非常感谢,但它在“let userList = lists.value as? [String: AnyObject]”行中有一些错误,错误显示“使用未解析的标识符'lists'”
    • 现在可以了!我改为“让 userList = product.value as?[String: AnyObject]” 非常感谢。如果我还有一个关于如何从 product_image_url 检索图像的问题,你介意吗?
    • 我尝试按照上面的链接进行操作,但仍然出现类似这样的错误,因为我想在 tableviewcell i.stack.imgur.com/WT14k.png987654323@的单元格中显示图片
    猜你喜欢
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2021-11-28
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多