0

[enter image description here][1]I'm trying to retrieve specific data from Database. I'd like to read all data in product_list

My Database

This is what i try but when i print listName. It show like this 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
    }

It have 4 things i want to retrieve

(details, name, price and product_image_url)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
POP
  • 17
  • 4

1 Answers1

0

Right now your code is looping over the child nodes of /Users/Sellers, which is the nodes of each individual user. What you seem to want to do is include all nodes under the product_list property of each user, which requires an extra nested loop:

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

})
POP
  • 17
  • 4
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank a lot but it have some error in line "let userList = lists.value as? [String: AnyObject]" the error show "Use of unresolved identifier 'lists'" – POP May 18 '19 at 02:48
  • Now it work! I change to "let userList = product.value as? [String: AnyObject]" Thank you so much. would you mind if i have one more question about how to retrieve image from product_image_url? – POP May 18 '19 at 03:29
  • See https://stackoverflow.com/questions/39398282/retrieving-image-from-firebase-storage-using-swift, https://stackoverflow.com/questions/39459555/swift-3-how-to-download-profile-image-from-firebase-storage – Frank van Puffelen May 18 '19 at 03:40
  • I try follow the link above but it still some error like this becuase i want to picture show in the cell of tableviewcell https://i.stack.imgur.com/WT14k.png – POP May 18 '19 at 09:37